简体   繁体   中英

Can't load context for Test

For some tests, I need to use prod-context and test-context. But Spring can't find my context.

I've tried to put context in a different location, also tried to upload context with various loader.

I have my context in the resources classpath.

Entities-src-test-java-Dao-test1
                -resources-META-INF-spring

My full path is C:\\Git\\Application\\Entities\\src\\test\\resources\\META-INF\\spring

When I try, for example

public class test1{
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("test-beans.xml");
...

or

@ContextConfiguration("file:/C://Git/Application/Entities/src/test/resources/META-INF/spring/test-beans.xml")
public class test1{
...

Or other various... I got error

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [test-beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [test-beans.xml] cannot be opened because it does not exist

If I start that class in debugger and check System.getProperty("user.dir") I will see C:\\Git\\Application or new File(".").getAbsolutePath(); shows me C:\\Git\\Application\\.

How to solve that?

Well ,it is not so good to make the location of the configuration file to depend on the file path. Are you sure other teammate will also put the project source codes in the folder C:\\Git\\Application\\Entities ? If not , your teammate most probably cannot run your test case out of the box , not to mention the build server.

Instead , you can try to refer the configuration file using classpath to prevent such file issues :

@ContextConfiguration("classpath:spring/test-beans.xml")
public class Test{


}

And put the test-beans.xml inside the folder C:\\Git\\Application\\Entities\\src\\test\\resources\\spring

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