【问题标题】:how to map a config class to application.properties in springboot in junit Test class?java - 如何在junit测试类的spring boot中将配置类映射到application.properties?
【发布时间】:2021-06-26 10:32:15
【问题描述】:

我想在junit测试类的springboot中将一个配置类映射到application.properties?我不想使用@Mock Annotation 或使用构造函数初始化配置类并使用setter 方法设置值。

【问题讨论】:

    标签: spring-boot junit junit5


    【解决方案1】:

    你在使用SpringExtension吗?您可以简单地将@EnableConfigurationProperties 放在您的测试类之上。确保使用 @ComponentScan@ContextConfiguration.classes 获取您尝试初始化的配置属性 bean。

    您还可以通过使用单独的配置文件(例如 test)、创建 application-test.properties 配置文件并将 @Profiles("test") 放在测试类的顶部来覆盖生产值。

    或者,如果您想避免为配置属性类准备嵌套模拟的繁琐,您可能想尝试deep stubbing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-05
      • 2021-05-22
      • 1970-01-01
      • 2021-12-23
      • 2023-01-15
      • 2014-12-29
      • 2019-08-23
      • 1970-01-01
      相关资源
      最近更新 更多