5

How can I create integrations tests using Gradle 5.2 and JUnit 5.3 in a multi-project build file?

This is my current build file:

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.10"
    classpath 'org.owasp:dependency-check-gradle:5.0.0-M2'
  }
}


allprojects {
  defaultTasks 'clean', 'build', 'publish', 'installDist'
  group = 'coyote'
  version = '0.1.0'
}

subprojects {

  apply plugin: 'java'
  apply plugin: 'jacoco'
  apply plugin: "com.github.spotbugs"
  apply plugin: 'org.owasp.dependencycheck'
  apply plugin: 'maven-publish'
  apply plugin: 'application'
  apply plugin: 'eclipse'
  apply plugin: 'idea'

  repositories {
    jcenter()
    mavenCentral()
  }

  dependencies {
    testImplementation ("org.junit.jupiter:junit-jupiter-api:5.3.2")
    testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.2")
    spotbugsPlugins ("com.h3xstream.findsecbugs:findsecbugs-plugin:1.8.0")
  }

  tasks.withType(Test) {
    useJUnitPlatform()
  }

  apply from: "$rootDir/integration-test.gradle"

  check.dependsOn jacocoTestCoverageVerification
  check.dependsOn dependencyCheckAnalyze

  spotbugs {
    effort = "max"
    reportLevel = "low"
    ignoreFailures = true
    showProgress = true
  }

  jacocoTestReport {
    reports {
      xml.enabled true
      html.enabled true
    }
  }

  check.dependsOn jacocoTestReport

  tasks.withType(com.github.spotbugs.SpotBugsTask) {
    reports {
      xml.enabled false
      html.enabled true
    }
  }

}

The integration-test.gradle file I am applying on line 46 contains the following:

sourceSets {
    itest {
        compileClasspath += sourceSets.main.output + configurations.testCompile
        runtimeClasspath += output + compileClasspath + configurations.testRuntime
    }
}

idea {
    module {
        testSourceDirs += sourceSets.itest.java.srcDirs
        testResourceDirs += sourceSets.itest.resources.srcDirs
        scopes.TEST.plus += [configurations.itestCompile]
    }
}

task itest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'
    testClassesDirs = sourceSets.itest.output.classesDirs
    classpath = sourceSets.itest.runtimeClasspath
    outputs.upToDateWhen { false }
}

Everything seems to work well within Intellij, but JUnit5 is not being added to the classpath, so any tests in the itest directories cannot find the JUnit libraries.

Running gradlew itest fails with similar results with the JUnit classes not being found.

I have tried to add use useJUnitPlatform() directly in the itest task, but with no success. I have also tried placing everything in the build.gradle file with no success.

Eventually, I'd like to use the same pattern to define load and security testing, running them separately as part of a CI/CD pipeline so placing everything neatly in their own directories under their respective projects is preferred to mixing everything in one test directory and using tags.

This is also helping other teams model CI/CD practices, so using Gradle 5 and JUnit 5 as designed is preferred as opposed to work-arounds or hacks to make things work. It's acceptable to learn that these versions don't work together or that there are currently defects\issues preventing this approach. Hopefully this is not the issue and I'm just missing something simple.

SCote
  • 664
  • 1
  • 8
  • 19

1 Answers1

3

The solution is I needed to include the JUnit 5 dependencies myself. The following is the correct integration-test.gradle file:

sourceSets {
    itest {
        compileClasspath += sourceSets.main.output + configurations.testCompile
        runtimeClasspath += output + compileClasspath + configurations.testRuntime
    }
}

idea {
    module {
        testSourceDirs += sourceSets.itest.java.srcDirs
        testResourceDirs += sourceSets.itest.resources.srcDirs
        scopes.TEST.plus += [configurations.itestCompile]
    }
}

dependencies {
    itestImplementation ("org.junit.jupiter:junit-jupiter-api:5.3.2")
    itestRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.2")
}

task itest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'
    testClassesDirs = sourceSets.itest.output.classesDirs
    classpath = sourceSets.itest.runtimeClasspath
    outputs.upToDateWhen { false }
}
SCote
  • 664
  • 1
  • 8
  • 19