How to get test coverage or ignore by Jacoco for Kotlin data classes
Asked Answered
G

4

11

Jacoco shows 0% coverage for Kotlin's data classes. How to configure it to measure coverage or ignore data classes at all?

Gunlock answered 23/1, 2018 at 8:19 Comment(0)
J
12

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).
Jahdai answered 2/9, 2018 at 11:53 Comment(0)
P
5

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")
}
Parson answered 20/3, 2018 at 3:9 Comment(2)
Though 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.Hellespont
It will test the logic. Only your statistics won’t show it.Shout
I
3

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.

Ignatia answered 23/1, 2018 at 8:39 Comment(3)
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.Gunlock
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.Ignatia
This should now be the accepted answer, as the mentioned issue is already fixed, and Jacoco no longer considers the auto-generated methodsGlottalized
F
1

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.

Fredericton answered 20/7, 2018 at 18:40 Comment(1)
discouraged from use as of the release of jacoco 0.8.2Tani

© 2022 - 2024 — McMap. All rights reserved.