使用@ActiveProfiles和SPRING_PROFILES_ACTIVE一起使用

4
在我的Spring Boot应用程序中,我有三个配置文件:默认配置文件、测试配置文件和持续集成配置文件。
持续集成配置文件定义了一种替代的datasource.url。
测试配置文件定义了一个替代的liquibase.change-log。
默认配置文件为我的应用程序定义了默认属性。
当我在本地运行测试时,我使用IntellIJ来运行它们。

enter image description here

为了在本地激活测试配置,我已经在我的测试中添加了@ActiveProfiles("test")注释。

当我从我的CI运行它们时,我已经像这样添加了SPRING_PROFILES_ACTIVE环境变量:

SPRING_PROFILES_ACTIVE=ci,test gradle integrationTest 

但Spring Boot忽略了ci配置文件。
如果我删除@ActiveProfiles("test")注释,我的CI可以正常工作,但我就不能轻松地使用IntelliJ运行测试了。
1个回答

2
当您在测试类中添加@ActiveProfiles("test")时,您将覆盖由SPRING_PROFILES_ACTIVE环境变量设置的活动配置文件定义。
  1. One option would be to add the "ci" profile to the test class execution:

     @ActiveProfiles({"test", "ci"})
    
  2. If activating "ci" for local/IDE test execution is not desirable, another option would be to remove the @ActiveProfiles annotation, and use -Dspring.profiles.active=test in your IDE run configuration for your test.

  3. But if you want profile "test" active on all tests, you could simply remove the @ActiveProfiles annotation, remove "test" from your env var CI job definition, and add the following line to application.properties/yaml inside the test/resources folder. (If you don't have test/resources/application.properties yet, know that it overrides main/resources/application.properties.)

     spring.profiles.include = test
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接