eclipse - 从 Eclipse 运行 Spring JUnit 测试时将/src/main/resources 添加到 Classpath

标签 eclipse spring unit-testing maven junit

有没有办法在 Eclipse 中获取 JUnit 测试(特别是我使用 SpringJUnit4ClassRunner)来使用来自 src/main/resources 以及 的资源src/test/resources?

Maven 的 surefire 插件可以做到这一点,但从 Eclipse 运行一个特定的单元测试却没有。

我在 src/main/resources/spring-config/ 中有大量 Spring 配置,我想“覆盖”两个特定文件。我已将这些特定于测试的覆盖放置在 src/test/resources/spring-config/ 中,并且当通过 Maven 运行单元测试时,一切正常。当我从 Eclipse 运行 JUnit 测试时,只有测试文件可用,不出所料,我的上下文无法加载。

更新

当使用通配符时,配置测试上下文的 Spring 类似乎无法从 src/main/resources/ 中找到资源,但 会在以下情况下找到它们我明确指定它们。

没有从 src/main/resources/

找到东西
@ContextConfiguration(locations = {"classpath:/spring-config/*.xml"})

是否src/main/resources/

找到指定文件
@ContextConfiguration(locations = {"classpath:/spring-config/*.xml",
                                   "classpath:/spring-config/app-aws.xml"})

最佳答案

这实际上是 Spring 的限制,而不是 JUnit。见 http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/resources.html#resources-wildcards-in-path-other-stuff了解详情。

解决方案是将您的 Spring 配置文件放在一个包中,而不是放在根包中。

关于eclipse - 从 Eclipse 运行 Spring JUnit 测试时将/src/main/resources 添加到 Classpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9632710/

相关文章:

Spring Boot CRON 表达式无法正常工作

java - Spring PDF 生成 + i18n

mysql - Spring Boot/Hibernate/MySQL 不工作

forms - 如何在测试 Angular 2 react 形式时使用 setValue 和 patchValue

java - 在 Java 中断言多线程代码的最佳方法是什么

eclipse - 在Eclipse中自动缩进新行-如何禁用?

java - 如何远程调试我的 rcp 应用程序?

eclipse - 无法验证 Weblogic 域”- Oracle Enterprise Pack

c++ - Android-ndk 与 eclipse : How to force reinstallation of apk

unit-testing - Windows Phone开发和单元测试