21

I use jacoco for coverage report. When I look at the jacoco report, coverage seems to be good. But in Sonarqube, the coverage is low because it says that @Dataannotation from lombok is not cover by test.

Compiled classes is mark as @Generated but it's not ignored by Sonar.

How can I make exclude @Data of the analysis ?

Martin Choraine
  • 2,296
  • 3
  • 20
  • 37
  • Have you tried this https://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-IgnoreIssuesonFiles? – mgyongyosi Aug 08 '17 at 13:31
  • No but ignore issues on file ignore the whole file. I don't want this behavior. I just want exclude method annotated with @Generated – Martin Choraine Aug 08 '17 at 13:37
  • Possible duplicate of [Filtering coverage with Lombok, Gradle, Jacoco and Sonar](https://stackoverflow.com/questions/48501606/filtering-coverage-with-lombok-gradle-jacoco-and-sonar) – Godin Jan 30 '18 at 22:34

4 Answers4

27

Godin's answer is correct but there is now a way to add that annotation automatically.

In order to do this you can create a lombok.config file in the root of your project and add this line in it:

lombok.addLombokGeneratedAnnotation = true

Full details here. As detailed in the documentation:

Lombok can be configured to add @lombok.Generated annotations to all generated nodes where possible; useful for JaCoCo (which has built in support), or other style checkers and code coverage tools: lombok.addLombokGeneratedAnnotation = true

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
10

According to JaCoCo changelog - starting from version 0.8.0 methods with annotation @lombok.Generated are automatically filtered out.

Changelog also notes:

Tools that directly read exec files and embed JaCoCo for this (such as SonarQube or Jenkins) will provide filtering functionality only after they updated to this version of JaCoCo.

Announcement of release of JaCoCo version 0.8.0 states:

Tools that directly read exec files (which is not a final report) and embed JaCoCo for generation of report will provide filtering functionality only after they updated to this version of JaCoCo. So please follow/wait/etc respective vendors such as

Reports generated by corresponding version (0.8.0) of integrations developed as part of JaCoCo project by us (Ant Tasks, Maven Plugin and Command Line Interface) provide filtering functionality.

As of today (30 Jan 2018) fix for https://jira.sonarsource.com/browse/SONARJAVA-2608 is supposed to be in not yet released SonarJava plugin version 5.1.

Godin
  • 9,801
  • 2
  • 39
  • 76
2

In general you cannot go ahead and exclude just one or two lines but, you can do something else which can get you 100% coverage.

The reason why you see an extreme drop in coverage after using @Data is that it contains a lot of annotations in it like @ToString, @EqualsAndHashCode, @Getter, @Setter, @RequiredArgsConstructor, etc. And sonar starts to look for all these methods which may or may not used by your bean/pojo.

For details on @Data annotation - https://projectlombok.org/features/Data

I am just assuming but, if you are using @Data annotation only for getters and setters then, use @Getter and @Setter annotations rather than @Data and I am sure that you will see a boost in your coverage.

Martin Choraine
  • 2,296
  • 3
  • 20
  • 37
Gunjal
  • 41
  • 3
0

tl;dr: My advice is to ignore the whole class from coverage.

Rationale: Most data classes do not contain ‘real code’. If a class consists solely of attributes and the @Data annotation, then you gain no advantage from the coverage. If you put logic in the class, it may not be a data class anymore – think about SRP.

That said, I once wrote a data class tester that tried to cover all the cases. In retrospect, it was not worth the trouble. Perhaps a library such as javabean-tester could be helpful, as well.

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112