Spring启动测试-重写bootstrap.properties

Spring启动测试-重写bootstrap.properties,spring,spring-boot,Spring,Spring Boot,我们在Spring启动应用程序中使用bootstrap.properties来配置Spring云配置相关属性 我们希望在测试期间忽略这些属性,因为我们不希望连接到配置服务器进行单元测试。因此,我们正在寻找一种从mainbootstrap.properties完全撤销属性的方法,并为测试或覆盖选择性属性提供一种新方法 我们尝试了创建src/test/resources/bootstrap.properties,src/test/resources/bootstrap test.properties

我们在Spring启动应用程序中使用bootstrap.properties来配置Spring云配置相关属性

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

我们尝试了创建
src/test/resources/bootstrap.properties
src/test/resources/bootstrap test.properties
spring.cloud.config.enabled=false
属性,但没有成功

在启动TestClass之前,我们尝试设置如下

static {
    System.setProperty("spring.cloud.config.enabled", "false");
}
但它不起作用

虽然Spring Boot文档很好地描述了application.properties的工作原理,但我甚至找不到对
bootstrap.properties
的任何引用

非常感谢您在测试期间以可靠的方式覆盖
引导.properties

(在这里回答我自己的问题)

经过多次尝试和错误发现,通过将spring配置文件设置为
test
,它实际上选择了
bootstrap test.properties
,并将其与主
bootstrap.properties
文件相结合

在本例中,设置
spring.cloud.config.enabled=false
仍在尝试引导,因为在主引导中它被设置为
spring.cloud.config.server.bootstrap=true
,所以我们必须在
bootstrap test.properties
中将此属性设置为false以完全禁用云服务器


希望这对某人有所帮助。

如果您正在使用
@springbootest
注释,您可以使用以下内容覆盖
引导程序中的属性

@SpringBootTest(properties = "spring.cloud.config.enabled=false")
否则,您可以:

  • @ActiveProfiles('test')
    添加到您的测试类中
  • 创建名为
    bootstrap test.properties的文件
  • 添加要覆盖的属性,例如
    spring.cloud.config.enabled=false
  • 更新:如果要禁用所有测试的spring cloud配置,只需在
    test/resources
    文件夹中创建一个
    bootstrap.properties
    ,并使用以下属性:


    spring.cloud.config.enabled=false

    对于手动测试,我已将local application.yml和bootstrap.yml文件添加到工作目录的根目录中。application.yml包含一行:

    spring.cloud.bootstrap.location: file:.
    

    此设置激活工作目录中的本地bootstrap.yml文件,并禁用类路径上的文件。来自

    的想法对我不起作用。默认情况下,
    bootstrap test.properties
    被忽略。我必须将
    @TestPropertySource(locations=“classpath:bootstrap test.properties”)
    添加到我的
    @springbootest
    类中。使用Spring Boot Starter 1.5.3.RELEASE。我做了以下操作:修改build.gradle以指定单元测试的Spring配置文件,如下所示:
    test{systemProperties System.properties systemProperties.put(“Spring.profiles.active”,“unittest”)}
    创建了
    bootstrap unittest.yml
    并将其放入
    src/test/resources
    。它配置为所有启用了
    spring.cloud
    的属性为false。@user909481
    引导测试。只有当您的测试具有
    @ActiveProfiles('test')
    时,才会自动拾取属性。如果您需要在所有测试中禁用它,请查看我的答案above@toddcscar这是可行的,但是如果你想禁用所有测试,你甚至不需要设置一个概要文件。(见上面我的答案)道格,回答得不错。只需指定@ActiveProfiles('test')并提供application-test.yml/application-test.properties就行了。不,SB 2.4.0+不起作用,只要有引导,它总是会先下载