使用SpringJUnit4ClassRunner找不到基于Spring注释的配置

时间:2013-08-30 12:32:57

标签: spring maven junit

我有一些使用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)

更新

我有maven-surefire-plugin <forkMode>never</forkMode>。 设置<forkMode>once</forkMode>(默认值)会使问题消失。

0 个答案:

没有答案