Run gradle task with Spring Profiles (Integration tests)

yazabara picture yazabara · May 30, 2017 · Viewed 9.3k times · Source

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.

Answer

lance-java picture lance-java · May 30, 2017

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.