spring - @ContextConfiguration无法在src/test/resources中加载配置文件

rob*_*mag 25 java junit spring maven-2 junit4

我尝试使用以下抽象类在src/test/resources类路径中加载spring配置文件:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/applicationContext.xml"})
public class BaseIntegrationTests {

}
Run Code Online (Sandbox Code Playgroud)

我在src/test/resources中有applicationContext.xml文件,但Spring无法加载它.

谢谢.

Pas*_*ent 26

确切地说,它是类路径上的测试输出目录(target/test-classes)的内容,而不是src/test/resources.但在资源src/test/resources被复制到测试输出目录resources:testResources目标(这是默认绑定的process-test-resources阶段).

话虽如此,您的代码看起来很好,测试源代码的资源应该由IDE或Maven在运行测试时复制,因此应该在类路径上可用.所以一定有别的错误.我可以看到你的类是集成测试的基类.你在pom中配置了什么花哨的东西吗?你能表现出来吗?


pra*_*kre 16

尝试使用*,以便它可以搜索您的类路径

@ContextConfiguration(locations={"classpath*:applicationContext.xml"})
Run Code Online (Sandbox Code Playgroud)

  • ```做什么比``{classpath:applicationContext.xml"`?搜索任何深度的类路径?我找不到解释这个的文档. (4认同)

Jes*_*ebb 6

有一个报告使用spring-test依赖项(包括SpringJUnit4ClassRunner)和JUnit> 4.4版本的错误.

如果您使用的是JUnit版本比4.4更新的,试图移动下来到4.4,看看它是否解决您的问题.