Use @ActiveProfiles together with SPRING_PROFILES_ACTIVE

Viewed 1026

In my Spring Boot application I have tree profiles: the default profile, a test profile and a continuous integration profile.

The continuous integration profile define an alternative datasource.url.

The test profile define an alternative liquibase.change-log.

The default profile define default properties for my application.

When I run tests locally I run them using IntellIJ

enter image description here

and to active the test profile locally I have added the @ActiveProfiles("test") annotation to my tests.

and when I run them from my CI I have add the SPRING_PROFILES_ACTIVE env var like this:

SPRING_PROFILES_ACTIVE=ci,test gradle integrationTest 

but the ci profile is ignored by Spring Boot.

If I remove the @ActiveProfiles("test") annotation my CI work perfectly but then I can not easily run my tests using IntelliJ.

1 Answers

When you add @ActiveProfiles("test") to your test class, you are overriding the active profiles definition set by the SPRING_PROFILES_ACTIVE env var.

  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
    
Related