The default code coverage engine to be used must be defined in the Global Settings page :

By default the java code coverage engine is Cobertura but out-of-the-box the Jacoco engine can also be used.
Cobertura
The Sonar Cobertura plugin is based on the cobertura-maven-plugin and so can only be used to compute code coverage information on Maven projects.
This plugin has only one parameter to define the maximum amount of memory used by the java process in charge to extract all code coverage information from the Cobertura report : 
In case the cobertura-maven-plugin is defined in the pom.xml of your project, Sonar will use the version defined in the pom and can potentially not function properly |
Jacoco
JaCoCo is an Open Source and robust java code coverage tool, developed by the authors of EclEmma. To use by default this code coverage engine, the property "sonar.core.codeCoveragePlugin" must have the value "jacoco".
The Sonar Jacoco plugin is part of the Sonar distribution only since Sonar 2.12. Prior to version 2.12, it was mandatory to explicitly install this plugin. |
Use Jacoco with Ant projects
Some additional configuration is required with Ant projects.
The Ant build.xml file must contain a dedicated target to launch either junit or testng unit tests. Let's take the following example with the target "test":
The nested task always has to declare fork="true", otherwise JaCoCo agent can't be attached. In addition task should declare forkmode="once" to avoid starting a new JVM for every single test case and decreasing execution performance dramatically (unless this is required by the nature of the test cases). |
Then, in this example, the value of the parameter "sonar.jacoco.antTargets" must be set to "test". During Sonar analysis, the Sonar Jacoco plugin will take care to attach the Jacoco agent to the JVM and to launch the unit tests.
Only specified targets would be executed. So for instance, if "test" target depends on a "compile" target, Jacoco plugin will only execute "test" target without executing first the "compile" target. In such case, a solution can be to update the "sonar" target in order declare a dependency on the "compile" target. |
Code coverage by Integration tests
The Sonar jacoco engine allows to feed Sonar with code coverage by integration tests.
To do this :
- Integration testing should be done with JaCoCo agent before execution of Sonar
- The "sonar.jacoco.itReportPath" property must be used when launching the Sonar analysis to define the path to the jacoco report generating by step 1
Additional links
- Comparison of Emma, Clover, Cobertura and JaCoCo
- Measure Code Coverage by Integration Tests with Sonar and JaCoCo
Known limitations
- Some libraries complain on classes, which were instrumented by JaCoCo, so exclusions must be set :
- Javassist (e.g. when Hibernate used) - "javassist"
- Drools - "org.drools.*"
- Exact number of line hits not available, because JaCoCo reports only status of line (no code, not covered, partly covered, fully covered) - see JaCoCo Coverage Counters.
