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

JavaGradleJunitCode CoverageJacoco

Java Problem Overview


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() {}
}

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

> 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 - Java

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 - Java

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

Solution 6 - Java

Following @mohamed-anees-a approach, I got to this kotlin version:

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class ExcludeFromJacocoGeneratedReport

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDon RhummyView Question on Stackoverflow
Solution 1 - JavaMohamed Anees AView Answer on Stackoverflow
Solution 2 - JavaAjinkyaView Answer on Stackoverflow
Solution 3 - JavaGodinView Answer on Stackoverflow
Solution 4 - JavaMarkus SchulteView Answer on Stackoverflow
Solution 5 - JavageniusView Answer on Stackoverflow
Solution 6 - JavaSilas PedrosaView Answer on Stackoverflow