简体   繁体   中英

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

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 {

}

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

Thank you.

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).

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?

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

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

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

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.

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

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

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.

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

如果你正在使用Maven并从eclipse运行测试用例,项目右键单击> Maven> maven update( ALT F5 )可能适合你。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM