JaCoCo Gradle plugin reports 0.0 class covered ratio for each package

4.9k views Asked by At

My directory structure is:

|/src
|   |/main
|   |  |/com.episo
|   |  |  |/contracts
|   |  |  |  |/clip
|   |  |  |  |/security
|   |  |  |/repositories
|   |  |  |  |/memory
|   |/test
|   |  |/com.episo
|   |  |  |/contracts
|   |  |  |  |/clip
|   |  |  |  |/security
|   |  |  |/repositories
|   |  |  |  |/memory

And here is the relevant section of my build.gradle:

repositories {
    mavenCentral()
    maven { url "http://dl.bintray.com/jetbrains/spek" }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    compile 'com.google.guava:guava:19.0'

    testCompile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    testCompile 'com.google.guava:guava:19.0'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    testCompile 'org.junit.platform:junit-platform-runner:1.0.0-M4'
    testCompile 'org.junit.platform:junit-platform-console:1.0.0-M4'
    testCompile ('org.jetbrains.spek:spek-api:1.1.2') {
        exclude group: 'org.jetbrains.kotlin'
    }
    testRuntime ('org.jetbrains.spek:spek-junit-platform-engine:1.1.2') {
        exclude group: 'org.junit.platform'
        exclude group: 'org.jetbrains.kotlin'
    }
}
// JaCoCo coverage rules
jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                minimum = 0.5
            }
        }

        rule {
            element = 'PACKAGE'
            limit {
                counter = 'CLASS'
                value = 'COVEREDRATIO'
                minimum = 0.7
            }
        }
    }
}

junitPlatform {
    filters {
        engines {
            include 'spek'
        }
    }

    enableStandardTestTask true
}

My code is written in Kotlin, and I am using Spek to write my unit tests. When I run my tests through the IntelliJ GUI, code coverage is properly reported. However, when I run gradle jacocoTestCoverageVerification, I get the following output:

* What went wrong:
Execution failed for task ':jacocoTestCoverageVerification'.
> Rule violated for bundle episo-contracts: instructions covered ratio is 0.2, but expected minimum is 0.5
  Rule violated for package com.episo.contracts: classes covered ratio is 0.0, but expected minimum is 0.7
  Rule violated for package com.episo.repositories: classes covered ratio is 0.0, but expected minimum is 0.7
  Rule violated for package com.episo.contracts.security: classes covered ratio is 0.0, but expected minimum is 0.7

Obviously, the 0.0 covered ratio for the packages is not correct; that would mean I haven't written any tests, which is not the case at all.

Is there maybe something about my directory structure that is making Jacoco not pick up on which tests match with which classes?

1

There are 1 answers

0
Somya Bansal On

For me this issue arrived when migrating a legacy project from Sringboot 2.3.x to 2.4.x and the issue turned out to be Junit5.

Had the similar issue that the tests were passing with gradle but jacoco complained about 0.0 coverage.

According to the release notes, adding this to the gradle build file solved it for me.