3

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.

Blauharley
  • 4,186
  • 6
  • 28
  • 47
  • 1
    Your question is interesting. Can you show what have you tried? – RAS Dec 21 '17 at 12:17
  • i have tried to edit the pom.XML with the sonar.coverage.exclusions Statement like this: *public int hashCode()*} , but it didn't work. @RAS – FengHong Zhang Dec 22 '17 at 09:05
  • I understand your point. Please edit your question & put all these details there. – RAS Dec 22 '17 at 09:33
  • As of May 2019, this appears to not be possible. https://community.sonarsource.com/t/is-there-a-way-to-exclude-java-equals-hashcode-methods-from-code-duplication/9118 – Chris Nelson Aug 08 '19 at 12:49

1 Answers1

2

(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/

Jeremy
  • 2,970
  • 1
  • 26
  • 50