【问题标题】:Add a spring profile to the existing ones in a SpringBootTest将弹簧配置文件添加到 SpringBootTest 中的现有配置文件
【发布时间】:2020-06-18 22:47:49
【问题描述】:

我有一个 SpringBootTest,我想为现有的一个弹簧配置文件添加一个特定的测试。我尝试了@SpringBootTest(webEnvironment = RANDOM_PORT, properties = "spring.profiles.include=my-second-profile"),这是在https://github.com/spring-projects/spring-boot/issues/7668 中建议的,但这根本不起作用。现在只有包含的配置文件处于活动状态。结果,我的上下文没有启动,因为缺少默认活动配置文件中的必需属性。有谁知道如何解决这个问题?

谢谢!

【问题讨论】:

  • 您的活动配置文件在哪里/如何定义?

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


【解决方案1】:

properties 需要一个数组 String[] 而不是单个对象 String。

@SpringBootTest(webEnvironment = RANDOM_PORT, properties = {"spring.profiles.include=my-second-profile"})

用{}包围你的字符串,它应该可以工作。

【讨论】:

  • AFAIK,即对于我使用过的所有其他注释,当数组中只有一个元素时,可以省略花括号。
【解决方案2】:

对我有用的方法是使用单独的 @ActiveProfiles 注释,而不必像你一样使用 properties。 @ActiveProfiles 需要一个以逗号分隔的配置文件名称字符串。所以你的注释可能变成如下:

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles(profiles = "main-profile,my-second-profile,other-profile")

【讨论】:

  • 问题是在stage-workstation(本地开发)和stage-ci(jenkins)上运行相同的测试,对于在jenkins上运行测试时激活的stage-ci,我需要一个通过 stage-ci 配置文件配置的代理。使用您的方法,配置文件将是静态的..但是感谢您的回答!
猜你喜欢
  • 2020-02-04
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2018-11-27
  • 2016-07-28
  • 2012-11-02
  • 1970-01-01
  • 2021-06-27
相关资源
最近更新 更多