从 Eclipse 运行 Spring JUnit 测试时将 /src/main/resources 添加到类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9632710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:48:48  来源:igfitidea点击:

Adding /src/main/resources to Classpath when Running Spring JUnit Tests from Eclipse

eclipsespringunit-testingmavenjunit

提问by EngineerBetter_DJ

Is there a way of getting JUnit tests in Eclipse (specifically I'm using SpringJUnit4ClassRunner) to use resources from src/main/resourcesas well as src/test/resources?

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

Maven's surefire plugin does this, but running one particular unit test from Eclipse does not.

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

I've got a load of Spring config in src/main/resources/spring-config/and I want to 'override' two specific files. I've placed these test-specific overrides in src/test/resources/spring-config/, and when running unit tests through Maven eveerything works. When I run a JUnit test from Eclipse, onlythe test files are available, and unsurprisingly my context fails to load.

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

Update

更新

Is appears the Spring classes that configure the test context can't find the resources from src/main/resources/when using a wildcard, but willfind them if I specify them explicitly.

出现配置测试上下文的 Spring 类src/main/resources/在使用通配符时无法找到资源,但如果我明确指定它们找到它们。

Doesn't find things from src/main/resources/

找不到东西 src/main/resources/

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

Doesfind specified files from src/main/resources/

是否src/main/resources/

@ContextConfiguration(locations = {"classpath:/spring-config/*.xml",
                                   "classpath:/spring-config/app-aws.xml"})

采纳答案by Kkkev

This is actually a limitation of Spring, rather than JUnit. See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/resources.html#resources-wildcards-in-path-other-stufffor details.

这实际上是 Spring 的一个限制,而不是 JUnit。有关详细信息,请参阅http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/resources.html#resources-wildcards-in-path-other-stuff

The solution is to place your Spring config files in a package rather than in the root package.

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