本文介绍了spring - @ContextConfiguration无法在src / test / resources中加载配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

}

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

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

谢谢。

推荐答案

准确地说,它是测试输出目录的内容 target / test-classes )位于类路径上,而不是 src / test / resources 。但目标(默认绑定到 process-test-resources 阶段)。

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/resources are copied to the test output directory by the resources:testResources goal (which is bound by default to the process-test-resources phase).

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

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?

这篇关于spring - @ContextConfiguration无法在src / test / resources中加载配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 04:18