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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2604875/
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-08-13 09:45:57  来源:igfitidea点击:

spring - @ContextConfiguration fail to load config file in src/test/resources

javaspringmaven-2junitjunit4

提问by robinmag

I've tried to load the spring config file in src/test/resources classpath with the following abstract class:

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

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

}

I have the applicationContext.xml file in src/test/resources but spring cant load it.

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

Thank you.

谢谢你。

采纳答案by Pascal Thivent

To be precise, it's the content of the test output directory(target/test-classes) that is on the class path, not src/test/resources. But resources under src/test/resourcesare copied to the test output directoryby the resources:testResourcesgoal (which is bound by default to the process-test-resourcesphase).

准确地说,是类路径上的测试输出目录( target/test-classes)的内容,而不是src/test/resources. 但是下面的资源src/test/resources会被目标(默认绑定到阶段)复制到测试输出目录中resources:testResourcesprocess-test-resources

Having that said, your code looks fine and resources for the test source code should have been copied either by your IDE or by Maven when running tests and should thus be available on the class path. So there must be something else wrong. I can see that your class is a base class for integration tests. Did you configure anything fancy in your pom? Can you show it?

话虽如此,您的代码看起来不错,测试源代码的资源应该在运行测试时由您的 IDE 或 Maven 复制,因此应该在类路径上可用。所以一定是有什么地方不对劲。我可以看到您的类是集成测试的基类。你在你的 pom 中配置了什么花哨的东西吗?你能展示一下吗?

回答by Bozho

You seem to be using maven, and trying to run the tests from within eclipse. Check the buil folder (target/test-classes/) for applicationContext.xml. If it is not there, you'd have to build first.

您似乎在使用 maven,并尝试从 eclipse 中运行测试。检查构建文件夹 ( target/test-classes/) 中的applicationContext.xml. 如果它不存在,则必须先构建。

回答by Jesse Webb

There is a reported bugwith using the spring-test dependency (includes SpringJUnit4ClassRunner) with versions of JUnit > 4.4.

在JUnit > 4.4 版本中使用 spring-test 依赖项(包括 SpringJUnit4ClassRunner)时报告了一个错误

If you are using a version of JUnit newer than 4.4, trying moving that down to 4.4 and see if it solves your problem.

如果您使用的 JUnit 版本高于 4.4,请尝试将其降低到 4.4,看看它是否能解决您的问题。

回答by benzen

I think i have a simillar problem, I found out that my application-context.xml was not on target/test-classes/ neighter on src/test/resources

我想我有一个类似的问题,我发现我的 application-context.xml 不在 src/test/resources 上的 target/test-classes/neighter

回答by prashant thakre

Try by using * so that it can search to your classpath

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

@ContextConfiguration(locations={"classpath*:applicationContext.xml"})

回答by borchvm

your application context must be included in classpath and put * :

您的应用程序上下文必须包含在类路径中并放置 * :

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

回答by Rupali

If you are using Maven and running test cases from eclipse, project right click > Maven > maven update (ALTF5)might work for you.

如果您使用 Maven 并从 Eclipse 运行测试用例,项目右键单击 > Maven > maven update ( ALTF5) 可能适合您。