Lombok • Exclude Generated Code From Test Coverage
Overview
When using Lombok in a Java project, the library generates code at compile-time, such as getters, setters, constructors, equals and hashCode methods, and more. This generated code can impact the accuracy of test coverage reports, since the generated code is not explicitly tested in unit tests.
To exclude the generated code from test coverage reports, you can use Maven Surefire plugin to exclude the generated code files from the coverage reports. This is accomplished by configuring the plugin to exclude files with the Lombok-generated suffix _*.java. By excluding these files from the coverage report, you can get a more accurate view of the actual test coverage of your project.
Excluding Generated Code From Report
When using Maven to generate test coverage reports, you may want to exclude the generated code from the report to get a more accurate view of the actual test coverage. Here’s how you can exclude generated code from test coverage when using Lombok with Maven:
Add the following plugin to your Maven pom.xml file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<excludes>
<exclude>**/*_.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
The **/*_.java
pattern in the excludes element will exclude all Java files generated by Lombok from the test coverage report. The _* suffix is used by Lombok to mark generated files.
With this configuration, the generated code will be excluded from the test coverage report, giving you a more accurate view of the actual test coverage.
Developer Pain Points
If generated code is not excluded from code coverage reporting tools, it can create false positives and inflate the code coverage metrics, leading to several developer pain points. Some of these pain points are:
Difficulty in identifying the actual code coverage:
If generated code is included in the code coverage reports, it becomes difficult to determine the actual code coverage. This can lead to a false sense of security and make it difficult to identify areas of the code that require more testing.
Maintenance overhead
Generated code is typically automatically generated and can change frequently. Including this code in the code coverage report can create additional maintenance overhead for developers as they need to constantly update the report.
Time-consuming
Including generated code in the code coverage report can increase the time required to run the code coverage tool. This can be particularly significant for large projects, which may have a lot of generated code.
Inaccurate results
Including generated code in the code coverage report can lead to inaccurate results as it is not the actual code written by the developers. This can make it difficult to identify areas of the code that require more testing and can also make it difficult to measure the effectiveness of testing efforts.
In Conclusion
Excluding generated code from test coverage reports is an important step in ensuring accurate and meaningful test coverage results for Java projects that use Lombok. This not only helps in getting a more accurate view of test coverage but also relieves developers from potential pain points. By following the steps outlined above and configuring the Maven Surefire plugin to exclude the generated code files, you can get a more accurate view of your test coverage and make more informed decisions about the quality of your code.
For More Info
For more info, visit the Lombok Project page at https://projectlombok.org/
Lombok • The Good, The Bad, and The Ugly
Post Date: 07 Nov 2023
Within the Java development community, Lombok often emerges as a polarizing subject. This library’s chief aim is to minimize the tedium of boilerplate code—a persistent thorn in the side of many Java developers. Nevertheless, every tool brings its own concessions to the table.
Lombok • @Value Annotation Best Practices
Post Date: 26 Oct 2023
When it comes to clean coding and enhanced testability in Java applications, Project Lombok is a game-changer. Its @Value annotation not only simplifies your code but also enhances its readability, maintainability, and testability.
Lombok • @Data Annotation Best Practices
Post Date: 26 Oct 2023
When it comes to clean and efficient Java coding, the power of Project Lombok cannot be overstated. Specifically, the @Data annotation provided by Lombok stands out as a valuable tool for Java developers.
Lombok val vs var
Post Date: 18 Oct 2023
Lombok has gained immense popularity among Java developers for its ability to simplify coding practices by reducing boilerplate code. In the vast ocean of features offered by Lombok, two features stand out: val and var. In this deep dive, we’ll uncover the secrets of these two variables and demonstrate their utility.
Lombok Test Coverage
Post Date: 21 Jun 2023
When it comes to software development, testing plays a crucial role in ensuring the quality and reliability of the codebase. Test coverage, in particular, is a metric that measures the extent to which the source code of a program has been tested.
Lombok Disadvantages
Post Date: 21 Jun 2023
In the world of Java development, optimizing code efficiency and reducing boilerplate is a constant pursuit. To address these challenges, various tools and libraries have emerged, and one such popular option is Lombok—a Java library that offers annotations for code generation, resulting in benefits such as time-saving and code simplification. However, as with any tool, there are both advantages and drawbacks to consider.
Lombok @Singular Annotation
Post Date: 01 May 2023
Lombok is a Java library that provides a set of annotations and utility classes to reduce boilerplate code in Java projects. One of its popular annotations is @Singular, which generates builder methods for collections.
Java • Avoid Getters & Setters Methods With Lombok
Post Date: 04 Apr 2023
This article provides an overview of how to avoid the repetitive code associated with writing getter and setter methods in Java classes using Lombok. By using Lombok’s annotations, such as @Getter and @Setter, developers can generate these methods automatically, thereby reducing the amount of boilerplate code required in their classes.
Lombok • @AllArgsConstruct vs @RequiredArgsConstructor
Post Date: 30 Mar 2023
What is the difference between @AllArgsConstruct and @RequiredArgsConstructor in Lombok? The main difference between @AllArgsConstructor and @RequiredArgsConstructor is in the fields that are included in the generated constructor.
Lombok • How to Use Constructor
Post Date: 30 Mar 2023
To use the constructor with Lombok, you can annotate your class with @AllArgsConstructor or @RequiredArgsConstructor. @AllArgsConstructor generates a constructor that takes in all the fields in the class as arguments, while @RequiredArgsConstructor generates a constructor that takes in only the final or @NonNull fields as arguments.
Lombok • An Overview
Post Date: 13 Mar 2023
Lombok is a Java library that provides annotations to help reduce boilerplate code and increase developer productivity. By using Lombok annotations, Java developers can automatically generate common code such as getters, setters, constructors, equals and hashCode methods, and more.
Lombok • Using @With Annotation to Clone Immutable Objects
Post Date: 29 Aug 2022
Using Lombok’s @With Annotation to Clone Immutable Objects is a beneficial feature that helps developers minimize code replication and ceremonial code. It is the next best alternative to Copy Constructs in object-oriented programming. The @With annotation also requires @AllArgsConstructor to function correctly.
Lombok • Builders and Copy Constructors
Post Date: 27 Dec 2021
Lombok’s builder and copy constructor feature using @Builder is a mechanism that allows the implementation of the Builder Pattern and Copy Constructors in Object-Oriented Programming. This article further illustrates how Lombok mitigates the disadvantages of creating builder methods and copy constructors making Lombok’s @Builder a good foundation for design and code maintainability.