JaCoCo coverage verification after multiple test Gradle tasks

Viewed 362

In my Java (Gradle-based) project, I have the default test task and custom defined integrationTest task. My build.gradle looks following

plugins {
    id 'de.jansauer.printcoverage' version '2.0.0'
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'jacoco'

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

sourceSets {
    integration {
        java.srcDir "$projectDir/src/integration/java"
        resources.srcDir "$projectDir/src/integration/resources"
        compileClasspath += main.output + test.output
        runtimeClasspath += main.output + test.output
    }
}

configurations {
    integrationImplementation.extendsFrom testImplementation
    integrationImplementation.canBeResolved true
    integrationRuntimeOnly.extendsFrom testRuntimeOnly
    integrationRuntimeOnly.canBeResolved true
}

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

idea {
    module {
        testSourceDirs += sourceSets.integration.java.srcDirs
        testResourceDirs += sourceSets.integration.resources.srcDirs
    }
}

check.dependsOn integrationTest

repositories {
    mavenCentral()
}

dependencies {
    testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.8.2'
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.8.2'
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.8.2'
    testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.4.6'
    testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '3.4.6'
    testImplementation group: 'org.mockito', name: 'mockito-inline', version: '3.4.6'
}

test {
    useJUnitPlatform()
    finalizedBy jacocoTestCoverageVerification, printCoverage
}

jacocoTestReport {
    reports.xml.enabled true
}

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                minimum = 1.0
            }
        }
    }
}

In general, it works fine, but there is a problem. Sometimes there are places in code that are really difficult to unit-test, and are great for integration tests. However, JaCoCo counts only coverage in unit tests. I could place finalizedBy jacocoTestCoverageVerification, printCoverage in the integrationTest task, but then I'd have two separate coverages.

My goal is to have one common coverage from both test configuration. I tried changing finalizedBy to dependsOn (with adequate logic change), but then I had no coverage at all. I don't know if it's even possible tbh, but I'm guessing it is.

0 Answers
Related