@Builder annotation blocks coverage.

185 views
Skip to first unread message

Dmitri Surname

unread,
Dec 5, 2024, 8:48:50 AM12/5/24
to Project Lombok
Hello Lombok community, when I have the following declarations:

@Data
@NoArgsConstructor
@Builder
@AllArgsConstructor
public class ClassNameDTO { ... }

when I run IntelliJ coverage configured for software of the Bank where I work, I see my "ClassNameDTO" covered as "0%".

Trying various solutions for two days that maked me crazy, I discovered that commenting "@Builder" my "ClassNameDTO" becomes covered as "100%"...

I MUST cover my software and I MUST have "@Builder"... How can I solve this problem? What bug do you have in your "@Builder" annotation code ???

Thank you very much,
Dmitri.


Simon Greatrix

unread,
Dec 5, 2024, 12:17:28 PM12/5/24
to Project Lombok
The "0%" and "100%" coverage are very suspicious. With just the @Builder removed you would still have the getter, setters, equals, hashcode and toString generated by the @Data annotation to test, plus the two specified constructors. Obtaining 100% coverage will require testing scenarios that one might say are "impossible". For example - Say you have a field annotated as non-null then Lombok will generate null checks in the builder, constructor, and setter. It is "impossible" to set the field to null, but the generated hashcode and equals methods will still include code to test for null. This is not a bug. Someone could use reflection to force the field to be null. That's why the "100%" is suspicious. You can also find oddities in the coverage reports, such as 100% lines covered but only 90% of the methods. This can happen because the Lombok generated code does not have line numbers in the original source, so some coverage tools do not include it when evaluating the percentage of lines covered. Then you get a less than 100% method coverage because, for example, the toString() on the Builder class was never invoked or something like that.

To move forward on this, first ensure your ClassNameDTO is checked into your source code control, so you can get it back. Then use Intellij's Delombok to remove the @Builder annotation and replace it with the code @Builder generates. This should be what your test cases see, so what coverage do you get?

Hope that helps,
Simon










Zoltán Csorba

unread,
Dec 9, 2024, 6:58:17 AM12/9/24
to Project Lombok
The 0% coverage looks suspicious. In the java projects I usually need to add lombok.config to the project root folder with
  1. lombok.addLombokGeneratedAnnotation = true

   This is enough for JaCoCo to omit the generated methods from the coverage report.

B  But if you have 100% when only the @Data annotation is used, then it might be a different issue. Still, it worth a  try if you don't have it. 
     Zoltan

Reply all
Reply to author
Forward
0 new messages