【发布时间】:2020-07-11 20:48:13
【问题描述】:
我正在使用具有 @PropertySource 注释的类 Propiedades 创建一个 TestClass。
@Component
@PropertySource("file:${services.properties}test-app/.properties")
public class Propiedades {
@Value("${db.name}")
public String dbName;
@Value("${db.jndi}")
public String dbJNDI;
@Value("${db.owner}")
public String dbOwner;
}
如您所见,我正在从相对于部署应用程序的服务器的外部位置加载.properties 文件。 services.properties 是服务器的 env 变量,它具有到特定文件夹的路由。
这是我的测试课
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Propiedades.class)
@TestPropertySource(locations = "/application-test.properties")
public class IncidenciaServicioServiceImplTest {
@Autowired
Propiedades propiedades;
@Test
public void isPropertySetup() {
String output = propiedades.dbName;
Assert.assertEquals("testDb", output);
}
}
运行测试时出现以下错误。
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [Propiedades]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'services.properties' in value "file:${services.properties}test-app/.properties"
为什么它试图从那个路径加载属性?,它不应该从src/test/resources/application-test.properties读取吗?
【问题讨论】:
标签: java spring-boot unit-testing properties-file