Search code examples
javamavengradlejunitjunit5

Separate integration and unit tests with Gradle


I'm migrating an existing Maven and JUnit 5 project where the integration and unit tests are separated into different phases:

  • the *Test.java unit tests are run on the test phase
  • the *IT.java integration tests are run on the verify phase

How do I accomplish the same separation with Gradle? It is my understanding that Gradle's check task is the equivalent to Maven's verify.


Solution

  • I ended up using filters to separate the unit and integration tests and to bind the integration tests to the check task:

    tasks.named('test') {
        useJUnitPlatform {
            filter {
                includeTestsMatching "*Test"
                excludeTestsMatching "*IT"
            }
        }
    }
    
    def integrationTest = tasks.register("integrationTest", Test) {
        useJUnitPlatform {
            filter {
                includeTestsMatching '*IT'
                includeTestsMatching 'IT*'
                includeTestsMatching '*ITCase'
            }
        }
    }
    
    tasks.named('check') {
        dependsOn integrationTest
    }