Search code examples
javaeclipseunit-testingcode-coverageeclemma

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.


Solution

  • First of all, @Data annotation is the combination of @ToString, @EqualsAndHashCode, @Getter, @Setter.

    If you just need Lombok to create getters and setters automatically, you can use only @Getter and @Setter annotations instead of @Data.

    Besides, to keep the methods created by Lombok outside of this coverage, you can create a lombok.config file in your root directory and have these two lines:

    config.stopBubbling = true
    lombok.addLombokGeneratedAnnotation = true
    

    After adding this line, when you go to Sonar, you will see that these classes are covered 100%.