There are two different ways It is possible to feed SonarQube with information on unit tests execution and code coverage :
Reusing Existing Reports
Every analyzer supports the reuse reports mode.
Depending on the code coverage tool you used to generate your report, replace jacoco by cobertura or clover in the property name.
Using SonarQube Embedded Engine
Before running an analysis with JaCoCo, the source code has to be compiled.
First, tell SonarQube to execute the unit tests and the code coverage tool:
Add this to your pom file:
With Maven, then, you just have to tell SonarQube which code coverage engine you wish to use: jacoco or cobertura or clover.
<sonar.java.coveragePlugin>jacoco | cobertura | clover</sonar.java.coveragePlugin>
Note that this property can be set at the SonarQube instance level through the web interface: Settings > Configuration > General Settings > Java.
- Maven and JaCoCo: projects/code-coverage/ut/maven/ut-maven-jacoco-runTests
- Maven and Cobertura: projects/code-coverage/ut/maven/ut-maven-cobertura-runTests
Maven Multi-module Project and JaCoCo
For multi-module Maven projects, compute overall code coverage by unit tests across modules. This case clearly lies between unit tests and integration tests but this is widely used.
Mapping of Unit Tests and Covered Code
The objective is to answer the following questions:
- Which files are covered by a given unit test?
- How many lines of code are covered by a given unit test?
- Which lines are covered by a given unit test?
- Which tests cover a given line of code?
See Component Viewer on Unit Test File for more details.
A sample project is available on github that can be browsed or downloaded: projects/code-coverage/ut/maven/ut-maven-jacoco-runTests. What you need to add to your pom.xml to get this behavior is contained between "BEGIN/END: Specific to mapping unit tests and covered code" tags in the pom file. Note that you have to run the analysis with the coverage-per-test profile. Read the README file for more information.
- JUnit 4.7+
- Maven Surefire Plugin 2.4+
This feature is only available while using JaCoCo as the code coverage tool.
0% code coverage reported, but unit tests are correctly executed
This problem occurs while using the Maven Cobertura Plugin and a special configuration of the Maven Surefire Plugin prevents the unit tests from being forked. This problem can be solved by removing the line "<forkMode>never</forkMode>" in the Maven configuration file (see SONAR-1445 and MCOBERTURA-70).
java.lang.Error: Unable to access JaCoCo Agent - make sure that you use JaCoCo and version not lower than 0.6.2
- Make sure that you do NOT set 'forkMode' to 'never' on the Surefire plugin - otherwise it will not fork the process and no JaCoCo agent will be attached.
... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.2</version> <configuration> <forkMode>never</forkMode> <!-- Do not set forkMode to never --> <reportFormat>xml</reportFormat> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> ...
- Make sure that you call first
mvn clean installand then
mvn sonar:sonar Pcoverage-per-testseparately. Indeed, only SonarQube can attach the JaCoCo agent, so
mvn clean install sonar:sonar -Pcoverage-per-testwill produce the error because the
testphase will be executed a first time alone.