Run gradle task with Spring Profiles (Integration tests)

Viewed 14069

Need to run tests via gradle with spring profiles.

gradle clean build

I've added task:

task beforeTest() {
    doLast {
      System.setProperty("spring.profiles.active", "DEV")
    }
}

test.dependsOn beforeTest

And my test definition is:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("TestProfile")
public class SomeTest {

But this construction doesn't work for me.

Gradle runs tests.

3 Answers

This works for me:

test {
    doFirst {
        systemProperty 'spring.profiles.active', 'ci'
    }
}

Now when I do gradlew test it runs with the ci profile.

Wonder if this is a very late reply? But I have encountered the similar problem with situation.

  1. Using @Profiles in springboot src/test/* folder.
  2. The @ActiveProfiles is actually activated (when viewing logs), but it doesn't use the @Profile when executed with gradle test. However, the @Profile was working in Eclipse Editor when JUnit test was executed.

So in the end i found that @Profile needs to be in src/main/* folder. Gradle test seems to have precedence over /src/main rather than /src/test. With this my @Profile("test") was converted into @Profile("!test"). There was no added spring.profiles.active into the gradle built.

Negating profile

Related