When I was playing around with Sonarqube, I noticed the the coverage is always 0.0%, although it does display the unit tests have all passed.
Then it shouldn't be 0.0%, should it?
At first, I tried specifying
the report path without any luck.
-Dsonar.junit.reportsPath=target/surefire-reports
Then I read a thread in which a guy had the same issue and he claimed to solve it by abandoning the default in-memory database in favor of an external database.
I tried that also to no avail.
At last, I came upon the official documentation regarding the unit test coverage, and it clearly states that
SonarSource analyzers do not run your tests or generate reports. They
only import pre-generated reports. Below you'll find language- and
tool-specific analysis parameters for importing coverage and execution
reports.
Wow, if only I had read it earlier.
For Java language, the popular tool to generate coverage report is Jacoco.
Thanks to this wonderful
tutorial, setting up Jacoco is made really easy.
All you need to do is copy the xml below under the build element of the pom.xml
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<!-- Prepares the property pointing to the JaCoCo runtime agent
which is passed as VM argument when Maven the Surefire plugin is executed. -->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution
data. -->
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec
</destFile>
<!-- Sets the name of the property containing the settings
for JaCoCo runtime agent. -->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!-- Ensures that the code coverage report for unit tests is
created after unit tests have been run. -->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution
data. -->
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec
</dataFile>
<!-- Sets the output directory for the code coverage
report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
<!-- Skips unit tests if the value of skip.unit.tests property
is true -->
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>
</plugins>
The maven command to generate the report is as simple as:
The maven command to run Sonarqube with Jacoco is:
mvn clean package sonar:sonar -Dsonar.projectKey=<key> -Dsonar.host.url=http://localhost:9000 -Dsonar.login=<token> -Dsonar.junit.reportsPath=target/surefire-reports -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-ut/jacoco.xml