Maven + Cobertura code coverage example_第1张图片

Cobertura is a free Java code coverage tool – calculates the percentage of code accessed by unit tests. In this tutorial, we will show you how to use Maven to generate the Cobertura code coverage report for your project.

1. Cobertura Code Coverage Report

Do nothing, just type the following Maven command to download and run the maven-cobertura-plugin automatically.

c:\project> mvn cobertura:cobertura
 
//...
Results :
 
Tests run: 16, Failures: 0, Errors: 0, Skipped: 0
 
[INFO]
[INFO] <<< cobertura-maven-plugin:2.6:cobertura (default-cli) @ TestNG <<<
[INFO]
[INFO] --- cobertura-maven-plugin:2.6:cobertura (default-cli) @ TestNG ---
[INFO] Cobertura 2.0.3 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Report time: 82ms
 
//Mkyong : Not sure what caused this error, but Cobertura still works well.
[ERROR] net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler 
loadCoverageData
 
INFO: Cobertura: Loaded information on 5 classes.
 
[INFO] Cobertura Report generation was successful.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.309s
[INFO] Finished at: Mon Jan 13 21:45:56 SGT 2014
[INFO] Final Memory: 25M/307M
[INFO] ------------------------------------------------------------------------

Maven will generate the Cobertura code coverage report at ${project}/target/site/cobertura/index.html.

More Examples
Please refer to this Cobertura Maven Plugin for more examples.

Figure : Sample of Cobertura code coverage report, index page, look like a JavaDoc.

Maven + Cobertura code coverage example_第2张图片

Figure : Detail page.

Maven + Cobertura code coverage example_第3张图片

2. Maven Site + Cobertura Report

To integrate Cobertura report into the Maven site, add the following to the reporting section.

pom.xml
//...
<reporting>
  <plugins>
	<!-- Normally, we take off the dependency report, saves time. -->
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-project-info-reports-plugin</artifactId>
		<version>2.7</version>
		<configuration>
			<dependencyLocationsEnabled>false</dependencyLocationsEnabled>
		</configuration>
	</plugin>
 
	// integrate maven-cobertura-plugin to project site 
	<plugin>
		<groupId>org.codehaus.mojo</groupId>
		<artifactId>cobertura-maven-plugin</artifactId>
		<version>2.6</version>
		<configuration>
			<formats>
				<format>html</format>
				<format>xml</format>
			</formats>
		</configuration>
	</plugin>
 
   </plugins>
</reporting>

Creating Maven project site

mvn site

Output – ${project}/site/index.html

Maven + Cobertura code coverage example_第4张图片

References

  1. Cobertura code coverage tool
  2. Cobertura Maven Plugin
  3. Maven – Creating a site
  4. Java Code Coverage Tools
  5. Stackoverflow : What is the proper way to use Cobertura with Maven
  6. Maven + Emma code coverage Example