Run gradle task with Spring Profiles (integration tests)

It is necessary to execute tests through gradle using spring profiles.

gradle clean build

      

I added a task:

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

test.dependsOn beforeTest

      

And my test definition:

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

      

But this construction doesn't work for me.

Gradle runs tests.

+3


source to share


2 answers


I think you want to set a system property in the JVM runtime / test, but you are setting the system property incorrectly in the build time JVM (i.e. the daemon

See Test.systemProperty (String, Object)

For example:



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

      

... and one more note about your attempt. Note that tasks have a method doFirst

and doLast

, so you do not need a separate task for what you are trying to do.

+3


source


This works for me:

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

      



Now when I do gradlew test

it starts up with a profile ci

.

0


source







All Articles