【问题标题】:spring boot how to autowire bean using yaml file in src/test/resource/spring boot 如何使用 src/test/resource/ 中的 yaml 文件自动装配 bean
【发布时间】:2017-04-25 15:39:04
【问题描述】:

我是 Spring boot(版本 1.1.8,spring 版本 4.0.7)的新手,我需要修复一个单元测试。被测类使用另一个具有简单自动装配配置 bean 的类。 bean 的类基本上是寻找一堆键值对,其中的键具有特定的前缀。使用 src/main/resources 中的 yaml 文件,代码本身可以正常工作。但是,当我运行测试用例时,此 bean 为空。如何让测试用例读取src/test/resources目录下的yaml文件?

【问题讨论】:

    标签: java spring spring-mvc spring-boot spring-test


    【解决方案1】:

    你可以为你的测试类使用@TestPropertySource注解:

    @TestPropertySource(locations = "classpath:your-tets-properties-file.yml")
    

    【讨论】:

      猜你喜欢
      • 2014-10-19
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2012-08-11
      • 2016-01-07
      • 2017-06-08
      • 1970-01-01
      相关资源
      最近更新 更多