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.
Join the DZone community and get the full member experience.
Join For FreeProblem
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!
Published at DZone with permission of Mladen Bolic. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments