【问题标题】:Override default Spring-Boot application.properties with application.properties用 application.properties 覆盖默认的 Spring-Boot application.properties
【发布时间】:2019-02-12 14:13:46
【问题描述】:

想用 application-test.properties 为我的 junit 完全替换我的 application.properties。 TestPropertySource 没有做这项工作。似乎它只替换了一些常见的属性,但我不想要。

想在我的 Junit \ 集成测试中加载 application-test.properties 而不是 application.properties

@RunWith(SpringRunner.class)
@ActiveProfiles( "test" )
@SpringBootTest(classes = Application.class)
@TestPropertySource(locations="classpath:application-test.properties")
@AutoConfigureMockMvc

【问题讨论】:

  • 不要以@SpringBootTest 运行它,而是构建您自己的弹簧配置测试。
  • 但我仍然只使用@ActiveProfiles( "test" )

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


【解决方案1】:

该注释有一个属性会抑制属性的继承:

@TestPropertySource(locations="classpath:application-test.properties", inheritProperties=false)

默认为true,因此您需要明确设置。

根据文档:

如果 inheritProperties 设置为 false,则 测试类将隐藏并有效替换任何内联属性 由超类定义。

【讨论】:

    【解决方案2】:

    我通常在我的测试文件夹 (src/test/resources) 中有一个资源文件夹,我在其中放置了修改后的 application.properties,spring-boot 应该使用它。请参阅https://www.baeldung.com/properties-with-spring 5.3

    【讨论】:

      猜你喜欢
      • 2015-06-22
      • 2020-03-04
      • 2020-06-22
      • 2020-05-03
      • 2015-09-12
      • 2017-01-29
      • 1970-01-01
      • 2015-05-18
      • 2018-04-12
      相关资源
      最近更新 更多