我有一些使用 Spring (3.1.3) 配置的 JUnit (4.10) 测试,如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TestConfig.class)
public class MyTest {...}
这些在 Eclipse 中运行良好,但使用命令行中的 Maven (surefire) 无法找到 TestConfig 类。我曾经使用基于 xml 的配置进行相同的配置,并且在这两种情况下都运行良好。我已经验证 TestConfig.class 在测试类中的预期位置。我还尝试将它移动到类中,以防上下文加载器没有查看测试类,但没有运气。有任何想法吗?
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
...
Caused by: java.io.FileNotFoundException: class path resource [com/test/TestConfig.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:49)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
at org.springframework.context.annotation.ConfigurationClassParser.findAllAnnotationAttributes(ConfigurationClassParser.java:282)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:225)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:148)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:134)
更新
我有<forkMode>never</forkMode>
maven-surefire-plugin。设置<forkMode>once</forkMode>
(默认)使问题消失。