java - 无法加载测试上下文

标签 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\.

如何解决?

最佳答案

嗯,让配置文件的位置依赖于文件路径不太好。您确定其他队友也会将项目源代码放在文件夹 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/

相关文章:

java - 需要配置Spring Security和Hibernate

javascript - 在 GitLab CI 作业上运行 TestCafe Docker Image 时出错。尝试在 BrowserStack 上执行自动化的端到端测试

python - 使用 Django RequestFactory 而不是表单数据的 POST 文档

java - HttpPost -> 重定向 -> 需要响应的位置或正文

java - 为什么接口(interface)是静态的?

java - 将 Spring 3.1 项目更新为 hibernate4 现在找不到当前线程的 session

android - 在 uiautomator 中滑动到下一页

java - Ubuntu 上带 PCSC lite 的 SCL01x 非接触式读卡器

java - 在jsp页面中显示特殊字符

java - Spring MVC 重定向是在 url 中添加一些参数