Jacoco shows 0% coverage for Kotlin's data classes. How to configure it to measure coverage or ignore data classes at all?
I was looking for the solution for same test coverage issue with auto-generated code for data model classes and stumbled upon following :
Kotlin + JaCoCo: Tuning Compiler to Skip Generated Code
Solution: Update JaCoCo plugin to 0.8.2 and your issues is resolved.
JaCoCo has solved this very issue in 0.8.2 release, please read the changelog-
- Classes and methods annotated with runtime visible and invisible annotation whose simple name is Generated are filtered out during generation of report (GitHub #731).
- Methods added by the Kotlin compiler that do not have line numbers are filtered out during generation of report. Idea and implementation by Nikolay Krasko (GitHub #689).
If you've put your data class under a specific package or in a specific file, you can exclude them from classDirectories
. In the example below, I've put data class under **/model/**
:
task kotlinJacocoTestReport(type: JacocoReport, dependsOn: 'test') {
reports {
html.enabled = true
html.destination = "${buildDir}/reports/jacoco"
}
sourceDirectories = files(["${project.projectDir}/src/main/kotlin"])
classDirectories = fileTree(dir: "${buildDir}/classes/kotlin/main", excludes: ['**/model/**'])
executionData = files("${buildDir}/jacoco/test.exec")
}
There is an open issue about it here, so automatic coverage filtering seems to be a work in progress.
EDIT There has been a pull request (accepted) introducing filtering for generated kotlin code. It did not find it's way into a release yet. But people have tested it in the SNAPSHOT version so far and everything seems to work.
Currently your best bet would be using the SNAPSHOT release.
Use the plugin as documented at: Kotlin plugin generated
The plugin removes all synthetic Kotlin generated code (automatic getters and setters and that should cleanly cover your data classes wherever they may be) from your code coverage by adding the @lombok.Generated annotation behind the scenes.
Note that you have to use Jacoco 0.8 or greater for it to work.
© 2022 - 2024 — McMap. All rights reserved.
getFullAddress()
which is rudimentarily adding up all the address fields. But if I exclude this, it won't test this logic. – Hellespont