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

标签 java spring maven-2 junit junit4

我尝试使用以下抽象类在 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/资源。但是 src/test/resources 下的资源被 resources:testResources 复制到 test 输出目录目标(默认绑定(bind)到 process-test-resources 阶段)。

话虽如此,您的代码看起来不错,并且测试源代码的资源应该在运行测试时由您的 IDE 或 Maven 复制,因此应该在类路径中可用。所以肯定有其他问题。我可以看到您的类是集成测试的基类。你有没有在你的 pom 中配置任何花哨的东西?可以展示一下吗?

关于java - spring - @ContextConfiguration 无法在 src/test/resources 中加载配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2604875/

相关文章:

java - Glide库报错: You cannot start a load from a destroyed Activity

java - 错误 MVC org.springframework.validation.BeanPropertyBindingResult : 1 errors with array

java - Spring-Boot 无法启动嵌入式 Tomcat

maven-2 - 在 Maven 中覆盖 'clean' 生命周期

maven - Maven中的继承和子模块概念有什么区别?

java - Java中的GUI计算按钮编码错误

java - DialogFragment - show() 方法

java - Spring AOP 切入点不适用于抽象方法

spring - 如何自定义 BigDecimal 的默认消息

maven-2 - Maven 在程序集中包含 JavaDoc Jar