Spring Boot 测试 - 覆盖 bootstrap.properties [英] Spring Boot test - overriding bootstrap.properties

查看:164
本文介绍了Spring Boot 测试 - 覆盖 bootstrap.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 Spring Boot 应用程序中使用 bootstrap.properties 来配置 Spring Cloud Config 相关的属性.

We are using bootstrap.properties in a Spring Boot app to configure Spring Cloud Config related properties.

我们想在测试期间忽略这些属性,因为我们不想连接到配置服务器进行单元测试.因此,我们正在寻找一种方法来从主 bootstrap.properties 中完全撤消属性,并提供一种新的方法来测试或覆盖选择性属性.

We want to ignore these properties during the testing as we don't want to connect to config server for unit testing. So we are looking for a way to completely undo properties from main bootstrap.properties and provide a new one for testing or override selective properties.

我们尝试使用 spring.cloud 创建 src/test/resources/bootstrap.propertiessrc/test/resources/bootstrap-test.properties.config.enabled=false 属性,但它不起作用.

We tried with creating src/test/resources/bootstrap.properties, src/test/resources/bootstrap-test.properties with spring.cloud.config.enabled=false property but it didn't work.

我们在开始TestClass之前尝试如下设置

we tried to set as below before starting the TestClass

static {
    System.setProperty("spring.cloud.config.enabled", "false");
}

它没有用.

虽然 Spring Boot 文档对 application.properties 的工作原理非常好,但我什至找不到对 bootstrap.properties 的引用.

While Spring Boot documentation is pretty good about how application.properties works, I couldn't find even one reference to bootstrap.properties.

非常感谢在测试期间以可靠的方式覆盖 bootstrap.properties 的任何帮助.

Any help is much appreciated on a reliable way to override bootstrap.properties during testing.

推荐答案

如果您使用 @SpringBootTest 批注,您可以使用以下内容覆盖 bootstrap.properties 中的属性以下:

If you are using the @SpringBootTest annotation you can override properties in the bootstrap.properties with the following:

@SpringBootTest(properties = "spring.cloud.config.enabled=false")

否则,您可以:

  1. @ActiveProfiles('test') 添加到您的测试类
  2. 创建一个名为bootstrap-test.properties
  3. 的文件
  4. 添加要覆盖的属性,例如spring.cloud.config.enabled=false
  1. Add @ActiveProfiles('test') to your test class
  2. Create a file named bootstrap-test.properties
  3. Add the properties you want to overwrite e.g. spring.cloud.config.enabled=false

更新:如果您想为所有测试禁用spring cloud配置,您只需在内创建一个bootstrap.properties>test/resources 文件夹,具有以下属性:

Update: If you want to disable spring cloud config for all tests, you can simply create a bootstrap.properties inside your test/resources folder with the following property:

spring.cloud.config.enabled=false

这篇关于Spring Boot 测试 - 覆盖 bootstrap.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆