Skip to content

Commit 0d724cf

Browse files
authored
Merge pull request #80 from nramc/41-add-notes-about-excluding-java-files-with-annotation
41 add notes about excluding java files with annotation
2 parents 8517591 + 87965cb commit 0d724cf

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: Excluding Java Code from JaCoCo Code Coverage Using Annotations
3+
description: Explore how to exclude Java code from JaCoCo coverage using annotations.
4+
author: Ramachandran Nellaiyappan
5+
date:
6+
created: 2025-03-17
7+
categories:
8+
- Java
9+
tags:
10+
- Java
11+
- JaCoCo
12+
- Code Coverage
13+
- latest
14+
hide:
15+
- toc
16+
links:
17+
- "[Author] Ram": https://nramc.github.io/my-profile/
18+
---
19+
20+
# Excluding Java Code from JaCoCo Code Coverage Using Annotations
21+
22+
In this article, we explore how to exclude Java code from JaCoCo coverage using **annotations**.
23+
24+
JaCoCo is a widely used code coverage tool for Java applications, helping developers assess test coverage. However,
25+
there are scenarios where certain methods or classes should be excluded from coverage reports, such as generated code,
26+
logging methods, or specific utility functions.
27+
28+
## Why Exclude Code from JaCoCo Coverage?
29+
30+
Some parts of the codebase should be excluded from test coverage calculations because they do not contribute to business
31+
logic or are externally managed.
32+
33+
Common cases include:
34+
35+
- **Generated Code:** Code generated by frameworks such as Lombok, JAXB, or MapStruct.
36+
- **Logging Methods:** Utility methods used exclusively for logging.
37+
- **Boilerplate Code:** Getters, setters, and other auto-generated methods.
38+
- **Test Utility Classes:** Helper methods used only for testing purposes.
39+
40+
## Excluding Code Using `@Generated` Annotation
41+
42+
JaCoCo automatically excludes methods and classes annotated with any annotation whose name ends with `Generated`.
43+
44+
This includes standard annotations like `javax.annotation.Generated` (Java 9+) and `jakarta.annotation.Generated` (Java
45+
17+),
46+
as well as any custom annotation that follows this naming pattern.
47+
48+
For Example, you can create a custom annotation like this:
49+
50+
```java
51+
52+
@Documented
53+
@Retention(RUNTIME)
54+
@Target({TYPE, METHOD})
55+
public @interface NoCodeCoverageGenerated {
56+
String description() default "";
57+
}
58+
```
59+
60+
```java
61+
import javax.annotation.Generated;
62+
63+
public class SampleClass {
64+
65+
@Generated
66+
public void generatedMethod() {
67+
// JaCoCo will ignore this method
68+
}
69+
70+
@NoCodeCoverageGenerated
71+
public void log() {
72+
// JaCoCo will ignore this method
73+
}
74+
}
75+
```
76+
77+
JaCoCo automatically detects and excludes such methods from coverage reports without requiring additional configuration.
78+
79+
## Excluding Classes/files in `jacoco.exec`
80+
81+
To exclude entire classes from coverage reports, configure the `jacoco-maven-plugin` as follows:
82+
83+
```xml
84+
85+
<plugin>
86+
<groupId>org.jacoco</groupId>
87+
<artifactId>jacoco-maven-plugin</artifactId>
88+
<version>0.8.8</version>
89+
<executions>
90+
<execution>
91+
<goals>
92+
<goal>prepare-agent</goal>
93+
</goals>
94+
</execution>
95+
</executions>
96+
<configuration>
97+
<excludes>
98+
<exclude>com/example/generated/**</exclude>
99+
</excludes>
100+
</configuration>
101+
</plugin>
102+
```
103+
104+
## Conclusion
105+
106+
Excluding specific methods or classes from JaCoCo reports is crucial to maintain accurate and meaningful coverage
107+
metrics. Since JaCoCo automatically excludes all methods annotated with any annotation ending in `Generated`, developers
108+
do not need additional configuration for this behavior.
109+
110+
By properly configuring JaCoCo, teams can focus on improving the quality of actual business logic while avoiding
111+
misleading coverage statistics.
112+

mkdocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ plugins:
7878
listings_tags_sort_by: !!python/name:material.plugins.tags.tag_name_casefold
7979
- pagetree
8080
- blog:
81-
post_date_format: short
81+
post_date_format: "d MMM Y"
8282
blog_toc: false
8383
post_dir: "{blog}/articles"
8484
post_url_format: "/{file}"

0 commit comments

Comments
 (0)