首页 > 解决方案 > 无法在 Groovy 测试用例中使用 Mock 从 yaml 文件中获取值

问题描述

我正在使用 Groovy 为我的功能编写一个单元测试用例。但是,我无法配置类中可用的值。这些值在我的 yaml 文件中配置。

这是我的代码

class UpdateServiceImplTest extends Specification {



     DataSourceRestTemplateConfig dataSourceRestTemplateConfig



    def setup() {
          dataSourceRestTemplateConfig= Mock(DataSourceRestTemplateConfig )

    }
}

此 DataSourceRestTemplateConfig 类正在使用一些属性,在执行测试时这些属性为 null

    public class DataSourceRestTemplateConfig  {

          @Autowired
          RestTemplate restTemplate;

          @Value("${datasource.auth.username}")
          private String userNameNew;

          @Value("${datasource.auth.password}")
          private String passwordNew;

            // Method to call DB here

  }

当我评估表达式时,上述值将变为 null。我还缺少其他配置吗?

任何想法都会对我有很大帮助。

标签: unit-testinggroovymockito

解决方案


推荐阅读