FengHong Zhang
FengHong Zhang

Reputation: 31

How to make sonarqube ignore the equals and hashcode

I'm wondering if it is currently possible to ignore the equals and hashcode method for the sonar test coverage? I have heard about the block exclusion, but it didn't work.

Upvotes: 3

Views: 4172

Answers (1)

Jeremy
Jeremy

Reputation: 3548

(assuming you're using jacoco for coverage reporting)

If you're not using Lombok, you might try adding the @Generated annotation to your methods you want skipped. I'm not sure this will work - but worth a shot!

If you're using Lombok [like I was], here's a solution from Rainer Hahnekamp that marks the code as @Generated, which makes jacoco ignore the methods, and in turn makes sonarqube display a higher coverage percentage.

Luckily, beginning with version 0.8.0, Jacoco can detect, identify, and ignore Lombok-generated code. The only thing you as the developer have to do is to create a file named lombok.config in your directory’s root and set the following flag:

lombok.addLombokGeneratedAnnotation = true

This adds the annotation lombok.@Generated to the relevant methods, classes and fields. Jacoco is aware of this annotation and will ignore that annotated code.

Please keep in mind that you require at least version 0.8.0 of Jacoco and v1.16.14 of Lombok.

https://www.rainerhahnekamp.com/en/ignoring-lombok-code-in-jacoco/

Upvotes: 2

Related Questions