Exclude Lombok method from test coverage

Posted by ChenRiang on November 27, 2021

Lombok is a nice Java library that reduces the boilerplate in our code. We can simply add in some annotations and Lombok will generate the code during compile-time. It was used heavily in my project. Recently I noticed these methods generated by Lombok are pulling down the unit test code coverage percentage in the report generated by Jacoco and SonarQube and I don’t want to spend time writing unit test for these class as it should be unit test properly in Lombok side.

Sample class:

1
2
3
4
5
6
7
8
9
import lombok.Data;

@Data
public class Person {

    private String name;
    private String gender;
    private int age;
}

Jacoco Report:


Root Cause

Lombok will generate code during compile time and these code is calculated in our unit test coverage report. All these class can simply contribute 10-20% unit test coverage, depending on your usage.


Solution

We can will configure Lombok to annotate the generated method with @lombok.Generated. Once the generated method is annotated with correct annotation Jacoco/SonarQube will filter out these class from the unit test coverage report.

To configure Lombok, add the following class into your project (same level with pom.xml).

lombok.config

1
2
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
  • config.stopBubbling - indicate that current location is root directory. By setting this to true, Lombok will stop searching for more config.
  • lombok.addLombokGeneratedAnnotation - annotate generated class with @lombok.Generated.

Result:

Person class is excluded from the report