无法加载测试环境

时间:2019-05-27 07:45:13

标签: java spring testing

对于某些测试,我需要使用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\Applicationnew File(".").getAbsolutePath();向我显示C:\Git\Application\.

如何解决?

1 个答案:

答案 0 :(得分:0)

好吧,让配置文件的位置取决于文件路径并不是很好。您确定其他队友也会将项目源代码放入文件夹C:\Git\Application\Entities中吗?如果不是,您的队友很可能无法立即运行您的测试用例,更不用说构建服务器了。

相反,您可以尝试使用classpath引用配置文件以防止出现此类文件问题:

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


}

并将test-beans.xml放在文件夹C:\Git\Application\Entities\src\test\resources\spring

相关问题