Thursday Oct 23

Key Concepts

Maven Cobertura

PDFPrintE-mail
Thursday, 21 April 2011 10:15
AddThis Social Bookmark Button

Maven Cobertura

Cobertura is a free code coverage tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage. Cobertura produce code coverage report. Code coverage was among the first methods invented for systematic software testing.

This Maven tutorial describes how to generate a test coverage report for a site using the Cobertura Maven Plugin.

Maven Cobertura Plugin

The Cobertura-Maven-Plugin is the maven plugin that analyzes how well your unit tests cover the code in your project source code. It generates a code coverage reportfor your project documentation site if you add a cobertura-maven-plugin entry to the reporting section of your pom.xml.

There are two aspects, first you need to add the plugin to the build part, then to the reports part

Maven Cobertura plugin configuration

<project>
    ...
    <build>
          <plugins>
               <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                          <formats>
                                <format>html</format>
                                <format>xml</format>
                          </formats>
                    </configuration>            
                  </plugin>    
          </plugins>
    </build>
    ...
</project>

Cobertura report

Cobertura report with Maven code coverage

<project>
    ...
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.5</version>
            </plugin>
            ...
        </plugins>
    </reporting>
</project>

Cobertura Features

  • Cobertura can be executed from ant or from the command line. 
  • Instruments Java bytecode after it has been compiled. 
  • Can generate reports in HTML or XML. 
  • Shows percent of lines coveraged and branches coveraged for each class, package, and for the overall project. 
  • Shows the McCabe cyclomatic code complexity of each class, and the average cyclomatic code complexity for each package, and for the overall product. 
  • Can sort HTML results by class name, percent of lines covered, percent of branches covered, etc. And can sort in ascending or decending order.

Generate Cobertura report

Cobertura report generation

mvn clean package cobertura:cobertura
mvn site

Problem with site generation

One of the most common problems with Cobertura is when generated report shows 100% test coverage while in reality many of the classes do not have tests. It is the case when you generate the Maven site. To prevent that you can use the following cobertura plugin configuration.

Cobertura Maven Site problem

<build>
        ....
    <plugins>
                 ....
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>site</id>
                    <phase>pre-site</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
                <execution>
                    <id>instrument</id>
                    <phase>site</phase>
                    <goals>
                        <goal>instrument</goal>
                        <goal>cobertura</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <instrumentation>
                    <excludes>
                        <exclude>com/ubiteck/ClassToIgnore.class</exclude>
                        <exclude>com/ubiteck/**/*IntegrationTest.class</exclude>
                        <exclude>com/ubiteck/**/*.class</exclude>
                    </excludes>
                </instrumentation>
            </configuration>

        </plugin>
    </plugins>
</build>


If you have any remark or questions feel free to put a comment.If you enjoyed this tutorial and want to promote it don't hesitate to click on



 

Tags: maven, code, project, site, plugin, cobertura, coverage, generate, cobertura-maven-plugin

Add comment


Security code
Refresh

Java Tutorial on Facebook