Spring Boot,通过集成测试用例读取yml属性
嗨,我正在使用Spring Boot" title="Spring Boot">Spring Boot,我想将.yml文件的值注入Bean中。我已经编写了集成测试用例,但是通过集成测试用例看起来它没有注入值。
问题是网址的值和keyspaceApp为null
豆
@ConfigurationProperties(prefix="cassandra")public class TestBean {
@Value("${urls}")
private String urls;
@Value("${keyspaceApp}")
private String app;
public void print() {
System.out.println(urls);
System.out.println(app);
}
public String getUrls() {
return urls;
}
public void setUrls(String urls) {
this.urls = urls;
}
}
集成测试用例
@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = TestBean.class)
@IntegrationTest
public class CassandraClientTest {
@Autowired
private TestBean bean;
@Test
public void test() {
bean.print();
}
}
申请yml文件
cassandra: urls: lllaaa.com
keyspaceApp: customer
createDevKeyspace: true
回答:
试试这个:
@SpringApplicationConfiguration(classes = TestBean.class, initializers = ConfigFileApplicationContextInitializer.class)
从其JavaDocs:
* {@link ApplicationContextInitializer} that can be used with the* {@link ContextConfiguration#initializers()} to trigger loading of
* {@literal application.properties}.
它说可以使用application.properties
,但是我想它也可以使用application.yml
。
以上是 Spring Boot,通过集成测试用例读取yml属性 的全部内容, 来源链接: utcz.com/qa/403913.html