How would I add an annotation to exclude a method from a jacoco code coverage report?
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?
Example:
public class Something
{
@ExcludeFromCodeCoverage
public void someMethod() {}
}
Solution 1:
Since there are no direct answers to this, did a bit of research and came across this PR.
https://github.com/jacoco/jacoco/pull/822/files
private static boolean matches(final String annotation) {
final String name = annotation
.substring(Math.max(annotation.lastIndexOf('/'),
annotation.lastIndexOf('$')) + 1);
return name.contains("Generated")
}
You can create any annotation with name containing "Generated". I've created the following in my codebase to exclude methods from being included in Jacoco report.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExcludeFromJacocoGeneratedReport {}
Use this annotation in your methods to exempt it from coverage as below.
public class Something
{
@ExcludeFromJacocoGeneratedReport
public void someMethod() {}
}
Solution 2:
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
Solution 3:
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.
Solution 4:
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.
Solution 5:
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