Jacoco shows 0% coverage for Kotlin's data classes. How to configure it to measure coverage or ignore data classes at all?
4 Answers
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).

- 1,052
- 2
- 13
- 25
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")
}

- 42,756
- 16
- 135
- 159
-
3Though this answers the question, this isn't fair to do so. E.g. I have a model class Address. I have a method inside it called `getFullAddress()` which is rudimentarily adding up all the address fields. But if I exclude this, it won't test this logic. – Rajkiran Dec 16 '19 at 15:59
-
It will test the logic. Only your statistics won’t show it. – Michael Piefel Jul 19 '22 at 13:46
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.

- 520
- 3
- 8
-
Hm. This annotation is never introduced. I didn't find it in sources also. But, I found filters implementation. But, it has two hardcoded annotations, and also it checks annotation for method only. It means I can not use my or existing annotation because I can not annotate the method because methods are generated. I have no ideas how to use it. – tse Jan 23 '18 at 09:48
-
You are right. Seems like the wiki is used for planning features only. Maybe you have to be patient then, or implement it yourself and submit a pull request. – Paul Georg Podlech Jan 23 '18 at 12:49
-
This should now be the accepted answer, as the mentioned issue is already fixed, and Jacoco no longer considers the auto-generated methods – marianosimone Jun 16 '18 at 05:02
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.

- 7,378
- 1
- 27
- 32