Getting meaningful coverage results in SonarQube when using JaCoCo and Lombok

When using the Lombok framework to generate the standard methods of a class, the coverage reported by SonarQube (and JaCoCo) may look inconsistent, or at least not so meaningful.
There are ways to configure Lombok to get better results.

Coverage results with no particular configuration for Lombok

When using Java code generation framework Lombok, and that you use JaCoCo for code coverage, the coverage reported in SonarQube is, without any specific configuration, a bit strange. Here’s below what you would typically get. In my example, this is strange because there is:

  • Apparently 4 lines to cover (and with the UT, 4 lines covered) ie 100% line coverage
  • Apparently 2 conditions to cover (the condition on line 13) and 1 covered ie 50% condition coverage
  • That would be in total (4+1)/(4+2) = 83.3% overall coverage

But as you can see from the screenshot below, SonarQube counts 1 condition covered out of 18 !.. and therefore 5.5% condition coverage and 22.7% overall coverage.

The problem does not come from SonarQube but from missing Lombok configuration to produce a proper JaCoCo coverage report (remember that SonarQube does not produce any coverage information, it’s merely reporting what’s provided in the coverage tools reports).
See below the JaCoCo HTML report (SonarQube is consistent with it)

Getting more meaningful coverage results
To get more meaningful results you must exclude the Lombok generated code from the calculation.
Note: Even if you have unit tests for the Lombok generated code, JaCoCo does not produce a report that will allow to properly take into account the coverage produced by these unit tests unfortunately, so you have no other option that excluding that code

To exclude the Lombok generated code you must add a *lombok.config file in the root directory of your project and add the following property in the file

lombok.addLombokGeneratedAnnotation = true

JaCoCo will automatically exclude the Lombok code from the coverage report and everything will look better, as below

Corner case: No code outside of the Lombok generated code

If you class does not contain any code except the lombok generated code (unlikely but who knows…) the effect of ignoring lombok code will yield a special result.

The jacoco.xml file is present and mentions the file, but with no lines to cover. Don’t be surprised: As a result SonarQube will display no coverage information (which is different from 0% coverage)

See for a sample project