Integration Testsets with Gradle

Some time ago I wrote a blogpost on our companies Blog about how we accomplished to create an integration-testset for our gradle projects. Since this post was written in german I wanted to translate and repost it here.

First you have to declare a new sourceSet for your integration tests in your build.gradle file. Additionally I’ve created the integrationTest task which can be used to execute the integration tests on their own.

// ...
apply plugin: 'java'

sourceSets {
    integrationTest {
        java {
            srcDir 'src/integrationTest/java'
        }
        resources {
            srcDir 'src/integrationTest/resources'
        }
        compileClasspath += sourceSets.test.compileClasspath
        runtimeClasspath += sourceSets.test.runtimeClasspath
    }
}

dependencies {
    // ....
}

task integrationTest(type: Test) {
    group = 'verification'
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
}

If you use IntelliJ IDEA as your IDEA make sure to upgrade to the version 2016.1 or higher. Otherwise it might happen that the gradle source sets don’t work as expected (see this blogpost by Jetbrains).

Now your unit and integration tests are completely seperated. You can execute your integration tests using the integrationTest task and your unit tests with the test task.