Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Description / Feature

Get code coverage for Java project with This plugin is included in the Java Ecosystem.

It provides the ability to feed SonarQube with code coverage data coming from JaCoCo (an open source and robust Java code coverage tool, developed by the authors of EclEmma). For  For more information, see: Comparison of Emma, Clover, Cobertura and JaCoCo.

SonarQube Java Plugin

1.1
1.21.31.41.52.02.12.2

Reports generated with JaCoCo

0.5.100.6.20.6.20.6.30.6.30.6.30.6.30.7.0

Usage

To launch JaCoCo as part of your Maven build, use this command: mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true

For more on JaCoCo, see its documentation.

You may also find the README.md of the sample project helpful.

See Code Coverage by Unit Tests for Java Project tutorial.

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.*"
  • The exact number of line hits is not available, because JaCoCo reports only status of line (no code, not covered, partly covered, fully covered) - see JaCoCo Coverage Counters.

Using argLine

If your project uses the argLine property to configure the surefire-maven-plugin, be sure that argLine defined as a property, rather than as part of the plugin configuration. Doing so will allow JaCoCo to set its agent properly. Otherwise the JVM may crash while tests are running.

That is, argLine should be defined this way: 

Code Block
	  <properties>
        <argLine>-Xmx128m</argLine>
      </properties>
...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <runOrder>random</runOrder>
        </configuration>
      </plugin>

 

Coverage per test 

Using some unit test listeners you can collect the information on which lines where covered by which tests and display them in SonarQube. See  the pom.xml of the sample project to configure this and see the Component Viewer documentation to display this information in SonarQube.