程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring Boot,通过集成测试用例读取yml属性大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring Boot,通过集成测试用例读取yml属性?

开发过程中遇到Spring Boot,通过集成测试用例读取yml属性的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring Boot,通过集成测试用例读取yml属性的解决方法建议,希望对你解决Spring Boot,通过集成测试用例读取yml属性有所启发或帮助;

试试这个:

@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文件的值注入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

大佬总结

以上是大佬教程为你收集整理的Spring Boot,通过集成测试用例读取yml属性全部内容,希望文章能够帮你解决Spring Boot,通过集成测试用例读取yml属性所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。