我正在尝试使用 Spring 创建单元测试。
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyConfig.class})
public class MyTest{
@Test
public void ...
}
要加载的类:
@ConfigurationProperties()
@PropertySource("config/myConfig.properties")
@Component
public class MyConfig {}
异常(exception):
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [de.db.sus.converter.fia.business.algorithm.config.FiaConverterConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/config/myConfig.properties]
我找到了用于 Web 应用程序和/或基于 xml 的配置的资源,但不适用于传输它们。
如果我使用 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
启动应用程序,属性将被加载。但我无法为每个单元测试启动整个应用程序。
我已验证该文件存在于目录 test/resources/config/中
最佳答案
似乎找不到请求的属性。我建议这样做:
如果请求的属性文件在您的类路径中,您可以通过编写下一行来修复上面的问题:
@PropertySource("classpath:config/myConfig.properties")
关于java - 测试上下文中的 "Could not open ServletContext resource",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39770694/