Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improve Lombok @Data Code Coverage

I am writing unit tests for my project and am trying to achieve at least 80% code coverage. Problem is that I am using lombok's @Data annotation for generating getters and setters and when I run my unit tests, all those getters and setters along with other methods like toString, equals, hashcode etc are missed and my code coverage takes a hit. Is there any workaround for this. I have been searching a lot about this but haven't been able to find anything which could help out. Any help on this would be appreciated.

I am using Eclemma for code coverage analysis.

like image 467
Varun Sharma Avatar asked Jun 16 '17 08:06

Varun Sharma


People also ask

How do you increase test coverage code?

Use Automation Tools and Frameworks Software test automation is no longer a hidden method for achieving faster coverage. Test automation frameworks such as Selenium, Appium, Cypress, etc. enable teams to run parallel tests for web and mobile apps faster.

What is the difference between @data and @value in Lombok?

@Value is the immutable variant of @Data ; all fields are made private and final by default, and setters are not generated. The class itself is also made final by default, because immutability is not something that can be forced onto a subclass.

What does @data Lombok annotation do?

@Data is a convenient shortcut annotation that bundles the features of @ToString , @EqualsAndHashCode , @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, ...


1 Answers

In 0.8.0 release, Jacoco added support for filtering out all methods annotated with @lombok.Generated from their reports. The only thing you need to change is to add lombok.config to the root of your project with the following settings:

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

And that's it. Jacoco will filter Lombok auto-generated methods, and if you give your best, your code coverage could be close to 100% :))

like image 119
mladzo Avatar answered Sep 18 '22 05:09

mladzo