【发布时间】:2014-09-21 09:05:47
【问题描述】:
我遇到了这样的 junit 测试问题。出于某种原因,只有当我将它们放在 maven 项目的 src/main/resources 文件夹中时,spring 上下文文件才有效。 Intellij 没有给我任何警告,但来自 mvn 和 ide 炸弹的 junit 测试运行器。
以下两种情况下的代码和输出:
@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);
}
}
如果文件位于 src/test/resources/test-applicationContext.xml 我明白了
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.
如果我将文件移动到 src/main/resources/test-applicationContext.xml 它会通过
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
为什么在测试上下文类路径中找不到该文件??
【问题讨论】:
-
您是否尝试将其放入适当的包装中,例如
classpath:com/mycompany/test-applicationContext.xml? -
同样的事情 - IDE 看到 spring 上下文但 JUnit 和 Maven 说文件不存在:IOException parsing XML document from class path resource [com/jg/datasharing/test-applicationContext.xml] ;嵌套异常是 java.io.FileNotFoundException:类路径资源 [com/jg/datasharing/test-applicationContext.xml] 无法打开,因为它不存在
-
我在代码旁边(例如 src/test/java/com/jg/datasharing)和资源中(src/test/resources/com/jg/datasharing)都试过了。 IDE 为我重构了路径,因此它似乎与实际配置相关,而不是(严格)人为错误。
标签: java xml spring maven junit