Java code coverage exclusion query

After several hours of online research including this forum, I am writing here to have a better understanding about Java code coverage with sonarqube and possibly find answers to some of my questions.

Before I start, my env consists of

  • Java 17 springboot applications
  • Jacoco maven plugin v0.8.7
  • Sonar maven plugin v3.9.0
  • Sonarqube Enterprise Edition v9.9.4

So as I understand, the code coverage is generated by Jacoco after unit tests and the report is transferred to sonarqube server by sonar maven plugin. While I am excluding some files from jacoco coverage report (checking locally from target/site/jacoco/index.html) by adding a exclude list under pom.xml > build > plugin > jacoco > configuration > excludes, I am also having a sonar.coverage.exclusions with the same files/classes.

Out of the above, I am trying to clarify the followings:

  • I am assuming the jacoco exclusion list is not accountable while building sonarqube code coverage. This is because the jacoco.exec / jacoco.xml is imported by sonarqube server and processed on its own to generate the code coverage % out of raw test result data.

  • Is there any way to use Java class annotations to mark code coverage exclusions inline in Java source code? I read somewhere about @Generated and some custom annotations but not too sure if that can be used for code coverage exclusion rather issue suppression?


Welcome to the community!

Thanks for this thoroughness! :smiley:

You’re two versions behind. I don’t think that’s impacting you, but you may want to bump up to version 3.11.

Yes, exactly. This is why you’ve needed to independently add coverage exclusions on the SonarQube side.

This seems entirely reasonable, especially the @Generated part, but I’m not finding anything about this, so I’m going to forward this to the language experts as a feature request.


1 Like

Thanks Ann for the response. Really appreciated and I am glad that my findings were valid. :slightly_smiling_face:

Hello @anupx73,

The @Generated annotation is sort of a hack for the JaCoCo. The coverage won’t be generated for the methods marked as @Generated. However, this was meant to be only for the truly generated code, but developers started using it to exclude some sources from coverage. AFAIK, This will work for any annotation containing the ''generated" word in the name.

While I understand your point and acknowledge that there’s a need to accept/exclude code coverage in the sources, I still think we should use the @Generated annotation for its real purpose.


1 Like