Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jacoco with gradle and kotlin is not creating jacoco.exec file

I've set up a basic kotlin project (empty classes, one test file) and have tried to set up jacoco with gradle using the plugin for gradle: https://docs.gradle.org/current/userguide/jacoco_plugin.html

When I run the tests, the one test passes, however there is no jacoco.exec file created. What am I missing? I assume the classes are never getting instrumented but can't seem to find much information on how to set up the build.gradle for jacoco. Or can jacoco not instrument class files created from kotlin?

group 'com.ronnev.filewatcher'
version '0.1'

buildscript {
    ext.kotlin_version = '1.1.61'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
    }
}

apply plugin: 'kotlin'
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'jacoco'

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"

    testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.2'

    testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.0.2'
}

jacoco {
    toolVersion = "0.7.9"
    reportsDir = file("$buildDir/jacoco/")
}

jacocoTestReport {
    classDirectories = files("${buildDir}/classes")

    reports {
        xml.enabled true
        csv.enabled false
        html.destination file("${buildDir}/jacoco/jacocoHtml")
    }
}

test {
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
    }
}

when I run gradle test I expect a build/jacoco/jacoco.exec file to be created but it is not.

vextorspace@vPrecise:~/projects/filewatcher $ gradle clean test
:clean
:compileKotlin
w: /home/vextorspace/projects/filewatcher/src/main/kotlin/com/ronnev/filewatcher/files/DependentFinder.kt: (6, 28): Parameter 'className' is never used
:compileJava UP-TO-DATE
:copyMainKotlinClasses
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestKotlin
:compileTestJava UP-TO-DATE
:copyTestKotlinClasses
:processTestResources
:testClasses
:junitPlatformTest

Test run finished after 71 ms
[         2 containers found      ]
[         0 containers skipped    ]
[         2 containers started    ]
[         0 containers aborted    ]
[         2 containers successful ]
[         0 containers failed     ]
[         1 tests found           ]
[         0 tests skipped         ]
[         1 tests started         ]
[         0 tests aborted         ]
[         1 tests successful      ]
[         0 tests failed          ]

:test SKIPPED

BUILD SUCCESSFUL

and when I look in the build directory there is no jacoco folder created:

vextorspace@vPrecise:~/projects/filewatcher $ ls build
classes  kotlin  kotlin-build  kotlin-classes  resources  test-results

Any help or hints would be much appreciated!

Thank you!

like image 345
vextorspace Avatar asked Oct 30 '25 03:10

vextorspace


1 Answers

The Jacoco is not yet able to attach itself to the new task names that JUint5 uses (e.g. junitPlatformTest).

You need to "help" Gradle to create the connection between the two tasks using the task.dependsOn(otherTask) method call.

In your case, adding this to your build.gradle may work:

jacoco {
    // This is the latest version at the time of writing this. Change as you need.
    toolVersion = '0.7.9'
    applyTo junitPlatformTest
}

test.dependsOn junitPlatformTest
like image 162
Mario Mueller Avatar answered Nov 02 '25 12:11

Mario Mueller