Take any Spring Boot project with unit tests and integration tests. I would like to run the unit tests separately, without losing the ability to run all tests at once. A solution to this question:
- runs both unit tests and integration tests by default from IntelliJ,
- supports a Gradle command that runs both unit tests and integration tests from the cli, and
- supports a Gradle command that runs only unit tests from the cli. It is important that the integration test classes are ignored entirely, because they contain external dependencies that are not available in the build environment.
My approach is to pass the test profile on the command line: gradlew test -PtestProfile=unit
Next, in build.gradle, set the active spring profile:
test {
if (project.hasProperty("testProfile")) {
systemProperty "spring.profiles.active", project.getProperties()["testProfile"]
}
}
The question: how do I annotate my integration test class, so that whenever spring.profiles.active=unit, the integration tests are skipped?
I tried:
@Profile, which does not work in test scope,@IfProfile, which does not work for scenario 1 because it requires an explicit property to be passed.@ActiveProfiles, which does not work because it overrides the currently active profile rather than conditionally disabling the test.
What is the correct way of doing this?
Aucun commentaire:
Enregistrer un commentaire