DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage
  • How To Implement Code Reviews Into Your DevOps Practice

Trending

  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  • Using LLMs to Automate Data Cleaning and Transformation Pipelines
  • Build a GitHub Slack Bot With AWS Bedrock and MCP, Part 2
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Improve Your Code Coverage With Lombok @Data

Improve Your Code Coverage With Lombok @Data

It turns out, when you add a new domain to a project in Jacoco, you are losing code coverage percentages. Click here to learn how to solve this with Lombok.

By 
Mladen Bolic user avatar
Mladen Bolic
·
Sep. 26, 18 · Presentation
Likes (6)
Comment
Save
Tweet
Share
42.9K Views

Join the DZone community and get the full member experience.

Join For Free

Problem

On my previous project, we had a problem achieving code coverage with Jacoco. Lombok was used extensively on the project, and each time we added a new domain object, we were losing precious code coverage percentages.

So, what was happening?

We were using @lombok.Data for generating equals/hashCode /toString methods for Java beans. The problem was that the code coverage tool was marking this method as uncovered. So, instead of having a Java bean with full code coverage, we ended up with around 15 percent coverage per Java bean. Adding new beans just made an already bad situation even worse.

Solution

Different solutions were proposed on how to fix this problem. One of them was using a third-party library to “cover” all equals/ hashCode/toString methods generated by Lombok. We’ve seen this solution being used on similar projects, but since we founded it was a dirty fix, we didn’t want to use it in our case. The solution we finally chose was actually very simple.

In the 0.8.0 release, Jacoco added support for filtering out all methods annotated with @lombok.Generated from their reports. The only thing we needed to do was tell Lombok to add the @lombok.Generated annotation to its generated methods.

In order to fix the issue, we added lombok.config file to the root of our project. The file had the following content:

config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true


  • config.stopBubbling = true is telling Lombok that this is the root directory and that it shouldn’t search parent directories for more configuration files (you can have more than one lombok config files in different directories/packages).
  • lombok.addLombokGeneratedAnnotation = true is telling Lombok to add the @lombok.Generated annotation to all generated methods.

And, that's it. After applying the given changes, Jacoco was filtering out Lombok auto-generated methods, code coverage was back to normal, and everybody was happy again!

Code coverage

Published at DZone with permission of Mladen Bolic. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage
  • How To Implement Code Reviews Into Your DevOps Practice

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook