13

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.

yazabara
  • 1,253
  • 4
  • 21
  • 39

3 Answers3

24

I think you are wanting to set a system property in the runtime/test JVM but you are incorrectly setting a system property in the build-time JVM (ie the Gradle daemon).

See Test.systemProperty(String, Object)

Eg:

test {
    systemProperty 'spring.profiles.active', 'DEV'
}

... and another note on your attempt. Please note that tasks have a doFirst and a doLast method so you wouldn't need a separate task for what you were attempting.

lance-java
  • 25,497
  • 4
  • 59
  • 101
0

This works for me:

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

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

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
  • 1
    You shouldn't do this in a `doFirst {...}`it's task config which should be done in the configuration phase, not the execution phase. See [build phases](https://docs.gradle.org/current/userguide/build_lifecycle.html#sec:build_phases) – lance-java Oct 03 '21 at 02:22
-1

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

Han
  • 728
  • 5
  • 17