Search code examples
javagradleintellij-ideaintegration-testing

Gradle: make integration test source module work


I have a simple multi-module Java Gradle project. The unit tests run fine, but I'm unable to add a working source set for integration tests. The tests are compiled (I see compile errors), and appear in the console output, but they are not executed (the existing tests should fail).

Furthermore, in IntelliJ IDEA, the source set is shown, but not as a test module. I see the "run" icons in the gutter next to the tests, but running a test gives > No tests found for given includes: [de.cotto.integration_tests.moduletwo.ModuleTest.name](filter.includeTestsMatching).

My questions:

  • What do I need to change in order to make the test(s) run (using ./gradlew build)?
  • How can I make IntelliJ IDEA pick up the source set as a "test" module?
  • How can I run the tests from IntelliJ IDEA?
  • Bonus points for Java 14 with modules (commit)

Source set definition:

sourceSets {
    integrationTest {
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}

configurations {
    integrationTestImplementation.extendsFrom testImplementation
    integrationTestRuntimeOnly.extendsFrom runtimeOnly
}

task integrationTest(type: Test) {
    description = 'Runs integration tests.'
    group = 'verification'

    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    shouldRunAfter test
}

check.dependsOn integrationTest
  • Gradle 6.4.1
  • JUnit 5
  • Java 14 and 11
  • With and without Java modules (JPMS, Jigsaw)
  • IntelliJ IDEA 2020.1.2
$ ./gradlew clean check --no-build-cache --console=plain
> Task :module-one:clean
> Task :module-two:clean
> Task :module-two:processResources NO-SOURCE
> Task :module-two:processTestResources NO-SOURCE
> Task :module-two:processIntegrationTestResources NO-SOURCE
> Task :module-one:compileJava
> Task :module-one:processResources NO-SOURCE
> Task :module-one:classes
> Task :module-two:compileJava
> Task :module-two:classes
> Task :module-one:compileTestJava
> Task :module-one:processTestResources NO-SOURCE
> Task :module-one:testClasses
> Task :module-two:compileTestJava
> Task :module-two:testClasses
> Task :module-two:compileIntegrationTestJava
> Task :module-two:integrationTestClasses
> Task :module-one:test
> Task :module-one:check
> Task :module-one:jar
> Task :module-two:test
> Task :module-two:integrationTest <-- What happens here? Why doesn't the test fail?
> Task :module-two:check

BUILD SUCCESSFUL in 2s
11 actionable tasks: 11 executed

Source code: https://github.com/C-Otto/gradle-integration-tests


Solution

  • Two pieces were missing. The following only defines JUnit for the test module, but not integrationTest. See https://discuss.gradle.org/t/integration-tests-not-being-run/28745.

    test {
        useJUnitPlatform()
    }
    

    As such, one of the following is necessary:

    integrationTest {
        useJUnitPlatform()
    }
    

    or

    tasks.withType(Test) {
        useJUnitPlatform()
    }
    

    Furthermore, the JUnit runtime wasn't available for the integration tests. This can be fixed by including the test runtime dependencies:

    Instead of integrationTestRuntimeOnly.extendsFrom runtimeOnly use integrationTestRuntimeOnly.extendsFrom testRuntimeOnly.

    If I add these two changes, the tests are executed with ./gradlew check. I'm also able to run the tests from inside IntelliJ IDEA.