SonarQube code coverage results do not match IDE

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    SonarQube * Developer Edition Version 8.0 (build 29455)
    <maven.surefire.version>3.0.0-M5</maven.surefire.version>
    <sonar.version>3.7.0.1746</sonar.version>
    <jacoco-maven-plugin.version>0.8.5</jacoco-maven-plugin.version>
    <junit.version>4.12</junit.version>
  • what are you trying to achieve
    Attempting to get accurate code coverage in SonarQube UI
  • what have you tried so far to achieve this
    I’ve tried multiple times to run all tests with coverage in IntelliJ and then run sonar.

Despite multiple attempts to get SonarQube to display accurate code coverage I continue to get inaccurate results in the UI. It has actually started showing less coverage than it did in the beginning despite the fact that I have added _more_coverage. The first of the below images shows the line numbers and code coverage for a method within IntelliJ, as you can see it shows the entire method is covered (there had been some gaps before). The second image shows the same method’s code coverage as reported by the SonarQube UI, as you can see it shows the entire method as not being covered. This particular file had started out with 9 new lines, 5 of which had no coverage. After I removed two unneeded lines taking the new lines down to seven and adding tests to cover the other lines it started showing that there were seven uncovered lines. The lines I removed had not been covered by tests to start with so even without the new tests it should have shown 7 new lines with 3 uncovered.

There are also at least four other files where I have added tests to cover all of the new lines and yet the UI still shows none of those lines as being covered.

Is there some trick I need to use to get SonarQube to update the coverage for specific files, or all the files?

I have been running all tests in the project through IntelliJ with coverage and then immediately running jacoco:prepare-agent jacoco:report sonar:sonar -Dsonar.login=…. I’ve lost count of how many times I’ve tried this after adding various tests to boost coverage in the project.

(Names have been redacted in the SonarQube image. -->)

Hey there.

SonarQube v8.0 is a fairly old version at this point. I would suggest upgrading to SonarQube v8.8.

If you absolutely cannot, or you are waiting for the upcoming v8.9 LTS:

  • Make sure the JaCoCo plugin is installed on your SonarQube instance (it’s bundled with SonarQube v8.8). This is responsible for reading JaCoCo reports. It doesn’t sound like any coverage is being reported for any files (correct me if I’m wrong), so wouldbe a likely culprit
  • Make sure your Java analyzer is the latest version compatible with SonarQube v8.0

You can check both these points in the Marketplace tab of your SonarQube instance (Administration > Marketplace)

I would also double-check that when you do this, the coverage reports are being generated with the expected coverage information before sonar:sonar is being run. The default location of these reports are usually one of:

target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml

1 Like