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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Does 100% Code Coverage Mean Tested?
  • 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

Trending

  • AI Agents Need an Identity Plane: Rethinking Enterprise Authorization for Autonomous Software
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • GenAI Security Gateway: An End-to-End Architecture

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Sep. 26, 18 · Presentation
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
43.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Does 100% Code Coverage Mean Tested?
  • 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

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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