Is there a way to exclude getter/setter when using jacoco with Kotlin?

Viewed 324

Is there a way to exclude methods like getter/setter/constructor from the scope of testing when using kotlin and jacoco together?

From jacoco 0.8.7 and Kotlin version 1.5 or later, it is said that if you use lateinit, it is excluded, but it is still included in the test report.

I've been struggling for a few days, please help if there is a way to solve it

This is the site I referenced

https://andrey-fomenkov.medium.com/kotlin-jacoco-tuning-compiler-to-skip-generated-code-935fcaeaa391 https://github.com/jacoco/jacoco/wiki/FilteringOptions

1 Answers

I've had success with the below approach.

As you may know, starting from Jacoco 0.8.2, you can exclude anything with @Generated from coverage. So we will use that.

The process is as follows

  1. Create CustomAnnotation Class
  2. Put in @Generated above Custom Getter/Setter get() or setter() in Example.kt

Qualifier.kt

@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.TYPE, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER)
annotation class Generated

Example.kt

val xxxxx: String
 @Generated
 get() = true
```
Related