Issue
I'm having a problem with junit test like this. For some reason spring context files only work if I put them in the src/main/resources folder of the maven project. Intellij doesn't give me any warning but both junit test runner from mvn and from ide bomb.
Code and output in both cases included below:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/test-applicationContext.xml"})
public class DataSharingTest {
AuthTokenService authTokenService;
@Autowired
public void setAuthTokenService(AuthTokenService authTokenService) {
this.authTokenService = authTokenService;
}
@Test
public void MyTest(){
System.out.println(authTokenService);
}
}
If the file is at src/test/resources/test-applicationContext.xml I get
2014-07-29 16:44:18,061 main ERROR TestContextManager Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@69e328e0] to prepare test instance [com.jg.datasharing.DataSharingTest@4090c06f]
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [test-applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [test-applicationContext.xml] cannot be opened because it does not exist. If I try a wildcard in the path it will stop complaining about file not found but will compalain that the bean is missing.
If I move the file to src/main/resources/test-applicationContext.xml it passes
2014-07-29 16:46:53,564 main INFO lBeanDefinitionReader Loading XML bean definitions from class path resource [test-applicationContext.xml]
2014-07-29 16:46:53,778 main INFO ricApplicationContext Refreshing org.springframework.context.support.GenericApplicationContext@7426dbec: startup date [Tue Jul 29 16:46:53 EDT 2014]; root of context hierarchy
2014-07-29 16:46:53,904 main INFO ltListableBeanFactory Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@19202d69: defining beans [authTokenService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
com.jg.security.token.auth.JwtAuthTokenService@1380cf2a
2014-07-29 16:46:53,995 Thread-1 INFO ricApplicationContext Closing org.springframework.context.support.GenericApplicationContext@7426dbec: startup date [Tue Jul 29 16:46:53 EDT 2014]; root of context hierarchy
2014-07-29 16:46:53,996 Thread-1 INFO ltListableBeanFactory Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@19202d69: defining beans [authTokenService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Process finished with exit code 0
Why would the file not be found in the test context classpath??
Solution
Check if the test/resources is added as classpath. You can verify in eclipse IDE as follows.
Right click on the project ->properties->java build path -> in Sources tab you should find {proejct}/src/test/resources (with option included all.)
If you cannot find the option you can manually add the directory or you can configure pom.xml as follows
<build>
<testResources>
<testResource>
<directory>${basedir}/src/test/resources</directory>
</testResource>
</testResources>
</build>
Answered By - Karthik Prasad
Answer Checked By - Timothy Miller (JavaFixing Admin)