Sonarqube coverage does not match the Jacoco report it has imported

Maven Plugin org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.0.2155
Sonarqube Developer Edition Version 9.2.1

I have a multi-module build. I’m producing an aggregate Jacoco report and setting sonar scanner to use that report.
One of the modules is only tested from tests in another module. The Jacoco aggregate report I created correctly has coverage shown for this. The sonar scanner when logged looks like it is using the report.

[INFO] 11:51:24.340 Sensor JaCoCo XML Report Importer [jacoco]
[INFO] 11:51:24.340 Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
[DEBUG] 11:51:24.340 Reading report '[...]/jacoco-aggregate/target/site/jacoco-aggregate/jacoco.xml'
[INFO] 11:51:24.382 Sensor JaCoCo XML Report Importer [jacoco] (done) | time=42ms

For what it’s worth the path to the report is configured using sonar.coverage.jacoco.xmlReportPaths

<sonar.coverage.jacoco.xmlReportPaths>${basedir}/jacoco-aggregate/target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>

When I view the Code coverage on SonarQube it shows 0% coverage for methods that are reporting >0% in the Jacoco xml report.

For example in the jacoco.xml one example method looks like

<method name="findUsersByFirstName" desc="(Ljava/lang/String;)[returnType];" line="280">
	<counter type="INSTRUCTION" missed="28" covered="63"/>
	<counter type="BRANCH" missed="2" covered="2"/>
	<counter type="LINE" missed="8" covered="15"/>
	<counter type="COMPLEXITY" missed="2" covered="1"/>
	<counter type="METHOD" missed="0" covered="1"/>
</method>

On SonarQube the entire class this method is in is showing 0.0% coverage.

Even though the Jacoco report is submitted, it is as if it is not used. Or that it is not able to reconcile the report with the code. I’m fairly certain the report is just not used, as even when I configure it to a missing location.

[INFO] 12:13:20.351 No report imported, no coverage information will be imported by JaCoCo XML Report Importer

The coverage on SonarQube is the regardless of whether or not a Jacoco report is submitted. Although maybe this is because if the report isn’t submitted SonarQube is relying on an old report? SonarQube is only showing coverage where the tests for a module are in the same module.

Could anyone recommend some likely causes of this issue? Is there any logs available on the server side I could look for that might explain why the code coverage on SonarQube is not like the report the scanner submitted?

1 Like

Hi,

Welcome to the community!

Do you always get this in your analysis log, regardless of the config value for the report path? If so, then I would wonder whether ${basedir} is being interpolated properly. You can see what property values analysis is getting by appending -Dsonar.scanner.dumpToFile=[path to file] to your analysis command line.

We also have a couple guides that may be useful:

 
HTH,
Ann

The report was imported. The example where it logs that it wasn’t was just a test I did to compare.

The report when imported logs like:

[INFO] 11:51:24.340 Sensor JaCoCo XML Report Importer [jacoco]
[INFO] 11:51:24.340 Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
[DEBUG] 11:51:24.340 Reading report '[...]/jacoco-aggregate/target/site/jacoco-aggregate/jacoco.xml'
[INFO] 11:51:24.382 Sensor JaCoCo XML Report Importer [jacoco] (done) | time=42ms

I think I managed to resolve the issue.

I managed to get the coverage for all modules to appear when I set the property sonar.coverage.jacoco.xmlReportPaths in each sub-module pom, instead of just in the parent pom. Based loosely on the example here https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven/maven-multimodule.

I’m not really clear how the sonar scanner is working. Is there any documentation that provides more details?

Hi,

There’s a setup guide:

Perhaps the problem is that you were configuring a report location at all. According to that guide:

By default the generated report will be saved under target/site/jacoco/jacoco.xml ; this location will be checked automatically by the sonar-jacoco plugin so no further configuration is required.

 
HTH,
Ann