spring – @ContextConfiguration无法在src / test / resources中加载配置文件
我尝试使用以下抽象类在src / test / resources类路径中加载spring配置文件:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:/applicationContext.xml"}) public class BaseIntegrationTests { }
我在src / test / resources中有applicationContext.xml文件但是Spring无法加载它。
谢谢。
确切地说,它是类路径上的测试输出目录 ( target/test-classes
)的内容,而不是src/test/resources
。 但是src/test/resources
resources下的src/test/resources
被resources:testResources
目标(默认情况下绑定到process-test-resources
阶段)复制到测试输出目录 。
话虽如此,您的代码看起来很好,测试源代码的资源应该由IDE或Maven在运行测试时复制,因此应该在类路径上可用。 所以一定有别的错误。 我可以看到你的类是集成测试的基类。 你有没有在你的pom中配置任何花哨的东西? 你能表现出来吗?
尝试使用*,以便它可以搜索到您的类路径
@ContextConfiguration(locations={"classpath*:applicationContext.xml"})
据报道 ,在JUnit> 4.4的版本中使用spring-test依赖项(包括SpringJUnit4ClassRunner)存在错误 。
如果您使用的是比4.4更新的JUnit版本,请尝试将其降低到4.4并查看它是否解决了您的问题。
您的应用程序上下文必须包含在类路径中并放入*:
@ContextConfiguration(locations = { "classpath:*/application-context.xml" })
你似乎正在使用maven,并尝试在eclipse中运行测试。 检查applicationContext.xml
的buil文件夹( target/test-classes/
)。 如果它不在那里,你必须先建立。
我想我有一个类比问题,我发现我的application-context.xml不在src / test / resources上的target / test-classes / neighter上
如果你正在使用Maven并从eclipse运行测试用例,项目右键单击> Maven> maven update( ALT F5 )可能适合你。