How would I add an annotation to exclude a method from a jacoco code coverage report?

后端 未结 4 923
庸人自扰
庸人自扰 2020-12-14 16:13

I have some code in Java that I want to exclude from code coverage. How would I do this? I want to be able to add an annotation. Is there a way to configure or extend jacoco

相关标签:
4条回答
  • 2020-12-14 16:19

    Tl;dr

    Use annotation @lombok.Generated from Lombok.

    Explanation

    Jacoco integrates with Lombok. Code generated by Lombok is excluded from Jacoco coverage by default (see Release 0.8.0 in Jacoco changelog). You can misuse lombok.Generated at your method for it being excluded from the coverage report.

    0 讨论(0)
  • 2020-12-14 16:19

    You can set lombok.addLombokGeneratedAnnotation = true into lombok.config in the root of project. After that, all Lombok-generated code will be ignored by Jacoco.

    See more in Project Lombok documentation: https://projectlombok.org/features/configuration

    0 讨论(0)
  • 2020-12-14 16:25

    The new feature has been added in the 0.8.2 release of JaCoCo which filters out the classes and methods annotated with @Generated. For details please see the documentation below:

    Classes and methods annotated with annotation whose retention policy is runtime or class and whose simple name is Generated are filtered out during generation of report (GitHub #731).

    JaCoCo 0.8.2 Release Notes

    0 讨论(0)
  • 2020-12-14 16:32

    I have some code in Java that I want to exclude from code coverage. How would I do this? I want to be able to add an annotation. Is there a way to configure or extend jacoco (as used in gradle) to use this?

    As of today there is no such feature in latest released version of JaCoCo (0.7.9). Only whole classes can be excluded.

    On page https://github.com/jacoco/jacoco/wiki/FilteringOptions#annotation-based-filtering (which is dedicated for developers) this is recorded as an idea for future versions.

    Official JaCoCo documentation contains information about how to obtain latest unreleased build as well as list of unreleased changes for next version - http://www.jacoco.org/jacoco/trunk/doc/changes.html , which includes various filters, among which filtering of methods that are generated by Lombok and Groovy and marked by annotations lombok.Generated and groovy.transform.Generated respectively. Potentially you can abuse this, but I wouldn't recommend to do so for many various reasons.

    0 讨论(0)
提交回复
热议问题