Skip to content

Excluding Java Code from JaCoCo Code Coverage Using Annotations

In this article, we explore how to exclude Java code from JaCoCo coverage using annotations.

JaCoCo is a widely used code coverage tool for Java applications, helping developers assess test coverage. However, there are scenarios where certain methods or classes should be excluded from coverage reports, such as generated code, logging methods, or specific utility functions.

Why Exclude Code from JaCoCo Coverage?

Some parts of the codebase should be excluded from test coverage calculations because they do not contribute to business logic or are externally managed.

Common cases include:

  • Generated Code: Code generated by frameworks such as Lombok, JAXB, or MapStruct.
  • Logging Methods: Utility methods used exclusively for logging.
  • Boilerplate Code: Getters, setters, and other auto-generated methods.
  • Test Utility Classes: Helper methods used only for testing purposes.

Excluding Code Using @Generated Annotation

JaCoCo automatically excludes methods and classes annotated with any annotation whose name ends with Generated.

This includes standard annotations like javax.annotation.Generated (Java 9+) and jakarta.annotation.Generated (Java 17+), as well as any custom annotation that follows this naming pattern.

For Example, you can create a custom annotation like this:

@Documented
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface NoCodeCoverageGenerated {
    String description() default "";
}
import javax.annotation.Generated;

public class SampleClass {

    @Generated
    public void generatedMethod() {
        // JaCoCo will ignore this method
    }

    @NoCodeCoverageGenerated
    public void log() {
        // JaCoCo will ignore this method
    }
}

JaCoCo automatically detects and excludes such methods from coverage reports without requiring additional configuration.

Excluding Classes/files in jacoco.exec

To exclude entire classes from coverage reports, configure the jacoco-maven-plugin as follows:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.8</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <excludes>
            <exclude>com/example/generated/**</exclude>
        </excludes>
    </configuration>
</plugin>

Conclusion

Excluding specific methods or classes from JaCoCo reports is crucial to maintain accurate and meaningful coverage metrics. Since JaCoCo automatically excludes all methods annotated with any annotation ending in Generated, developers do not need additional configuration for this behavior.

By properly configuring JaCoCo, teams can focus on improving the quality of actual business logic while avoiding misleading coverage statistics.


Did this post help you? Share on: X (Twitter) Facebook LinkedIn reddit WhatsApp Hacker News