javasonarqubejacocosonarqube-scan

Java Code Coverage with both Jacoco & SonarQube


We have a java project that runs in our local and in cloud after deployment. To perform Code Coverage we use both Jacoco & SonarQube as mentioned below:

  1. In local we use Jacoco to verify the test coverage and generate report. Jacoco configurations are specified in POM file. **We also have some configurations to exclude some files from calculating the coverage in below 2 different ways: **

    • using configuration & excludes elements in POM fileto exclude some packages & classes from code coverage.
    • using @Generated custom annotation whose Retention Policy is RUNTIME.
  2. Also, we use Sonar in the Jenkins pipeline to calculate the code coverage. And in jenkins file, we have below properties:

    "sonar.java.coveragePlan": "jacoco"
    "sonar.coverage.jacoco.xmlReportPaths": "***/target/site/jacoco/jacoco.xml"
    

So, my question is- I already mentioned some java classes in POM file, that needs to be excluded from the Jacoco Code Coverage and does sonar is going to run the code coverage on its own (OR) does sonar is going to use the code coverage report generated by the Jacoco? Also, will sonar be able to recognize the packages & classes that are excluded from the Jacoco code coverage (OR) do we need to explicitly tell sonar on what all files to exclude from the Code Coverage?


Solution

  • Jacoco generates the data report, and SonarQube ingests that report. If the overall process isn't producing results close to what you expect, the first thing to verify is what Jacoco is producing. It generates an html file tree that you can open in a browser to see what results it got. Once that is generating correct data, make sure that SonarQube can find that data.