使用 spring 云在 spring 引导应用程序中进行集成测试

Integration tests in spring boot application with spring cloud

我有一个使用配置服务器的 spring-boot 应用程序。 在项目中我有一个 bootstrap.yml:

spring:
 cloud:
    config:
      uri: ${CLOUD_CONFIG_URI:http://localhost:8888}
      failFast: true
      enabled: ??

我可以通过参数传递服务器位置的实际配置。没关系。

有了这个配置,我不知道如何在集成测试中禁用它。我的测试加载此配置并希望与配置服务器通信。我知道我可以通过 spring.cloud.config.enabled=false 但这不是一个解决方案(我想在 IDE 和 运行 测试中右键单击每个测试方法无需额外配置)。

有什么想法吗?

这是一个迟到的答案,但对于从任何搜索引擎加入的人来说,这是我的解决方案:

集成测试上方的 @Profile("test") 注释 class 是正确的。要让它真正起作用,是将两个额外的配置文件添加到您的普通资源文件夹,而不是测试资源文件夹。

  1. 添加"application-test.yml"
  2. 添加"bootstrap-test.yml"

在您的 bootstrap-test.yml 中添加以下内容:

spring:
    cloud:
        config:
          enabled: false

使用此配置,您可以将所有需要的配置选项放入应用程序-test.yml,并且 bootstrap-test.yml 将禁用 spring 云配置。