对于某些测试,我需要使用 prod-context 和 test-context。但是 Spring 找不到我的上下文。
我尝试过将上下文放在不同的位置,也尝试过使用各种加载器上传上下文。
我在资源类路径中有我的上下文。
Entities-src-test-java-Dao-test1
-resources-META-INF-spring
我的完整路径是
C:\Git\Application\Entities\src\test\resources\META-INF\spring
当我尝试时,例如
public class test1{
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("test-beans.xml");
...
或
@ContextConfiguration("file:/C://Git/Application/Entities/src/test/resources/META-INF/spring/test-beans.xml")
public class test1{
...
或者其他各种...我有错误
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
如果我在调试器中启动该类并检查
System.getProperty("user.dir")
我会看到 C:\Git\Application
或 new File(".").getAbsolutePath();
显示 C:\Git\Application\.
如何解决?
最佳答案
嗯,让配置文件的位置依赖于文件路径不太好。您确定其他队友也会将项目源代码放在文件夹 C:\Git\Application\Entities
中吗?否则,您的队友很可能无法开箱即用地运行您的测试用例,更不用说构建服务器了。
相反,您可以尝试使用类路径引用配置文件以防止此类文件问题:
@ContextConfiguration("classpath:spring/test-beans.xml")
public class Test{
}
并将 test-beans.xml
放入文件夹 C:\Git\Application\Entities\src\test\resources\spring
关于java - 无法加载测试上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56321759/