Cobertura and Maven: Code Coverage for Integration and Unit Tests

On the turmeric project, we maintain a nightly dashboard. On the dash board we collect statistics about the project, including code coverage, findbugs analysis and other metrics. We had been using the Maven EMMA plugin to provide code coverage, but ran into a problem with EMMA. It was causing test failures after the classes were instrumented. So we disabled the code coverage, as we needed accurate test results during our builds. However we still needed code coverage, more importantly we also need coverage for the existing test suite, which really an integration test suite instead of a unit test suite.

Cobertura and EMMA plugins both really are designed to work with unit tests. So we have to work around the limitation.

  1. First we need to instrument the classes.
  2. Second we need to jar up the instrumented classes and have them used by the build later.
  3. Need to tell the Integration Tests to use the instrumented classes for it’s dependencies.
  4. Generate an XML report of the results.

I tried doing this without falling back to ant, but everytime I tried to use the maven-site-plugin and configure it to generate the reports, it would complain that cobertura:check wasn’t configured correctly. In our case I didn’t need check to run, I just needed the reports generated. So Ant and AntContrib to the rescue. The following is the complete maven profile I came up with:

     <profile>
         <id>cobertura</id>
         <dependencies>
            <dependency>
               <groupId>net.sourceforge.cobertura</groupId>
               <artifactId>cobertura</artifactId>
               <optional>true</optional>
               <version>1.9.4.1</version>
            </dependency>
         </dependencies>
         <build>
            <plugins>
               <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>cobertura-maven-plugin</artifactId>
                  <configuration>
                     <instrumentation>
                        <excludes>
                           <exclude>org/ebayopensource/turmeric/test/**/*.class</exclude>
                           <exclude>org/ebayopensource/turmeric/common/v1/**/*.class</exclude>
                        </excludes>
                     </instrumentation>
                  </configuration>
                  <executions>
                     <execution>
                        <id>cobertura-instrument</id>
                        <phase>process-classes</phase>
                        <goals>
                           <goal>instrument</goal>
                        </goals>
                     </execution>
                  </executions>
               </plugin>
               <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-jar-plugin</artifactId>
                  <executions>
                     <execution>
                        <id>cobertura-jar</id>
                        <phase>post-integration-test</phase>
                        <goals>
                           <goal>jar</goal>
                        </goals>
                        <configuration>
                           <classifier>cobertura</classifier>
                           <classesDirectory>${basedir}/target/generated-classes/cobertura</classesDirectory>
                        </configuration>
                     </execution>
                  </executions>
               </plugin>
               <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-install-plugin</artifactId>
                  <version>2.3.1</version>
                  <executions>
                     <execution>
                        <id>cobertura-install</id>
                        <phase>install</phase>
                        <goals>
                           <goal>install</goal>
                        </goals>
                        <configuration>
                           <classifier>cobertura</classifier>
                        </configuration>
                     </execution>
                  </executions>
               </plugin>
               <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-antrun-plugin</artifactId>
                  <executions>
                     <execution>
                        <phase>verify</phase>
                        <configuration>
                           <tasks>
                              <taskdef classpathref='maven.runtime.classpath'
                                 resource='tasks.properties' />
                              <taskdef classpathref='maven.runtime.classpath'
                                 resource='net/sf/antcontrib/antcontrib.properties' />
                              <available
                                 file='${project.build.directory}/cobertura/cobertura.ser'
                                 property='ser.file.exists' />
                              <if>
                                 <equals arg1='${ser.file.exists}'
                                    arg2='true' />
                                 <then>
                                    <echo message='Executing cobertura report' />
                                    <mkdir
                                       dir='${project.build.directory}/site/cobertura' />
                                    <cobertura-report
                                       format='xml'
                                       destdir='${project.build.directory}/site/cobertura'
                                       datafile='${project.build.directory}/cobertura/cobertura.ser' />
                                 </then>
                                 <else>
                                    <echo message='No SER file found.' />
                                 </else>
                              </if>
                           </tasks>
                        </configuration>
                        <goals>
                           <goal>run</goal>
                        </goals>
                     </execution>
                  </executions>
                  <dependencies>
                     <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>20020829</version>
                     </dependency>
                  </dependencies>
               </plugin>
            </plugins>
         </build>
      </profile>

Note: Do not use the cobertura:cobertura goal with this profile. It will fail the build because it will try to instrument the classes twice.

The use of Ant and AntContrib was a necessity because there is no cobertura:report goal, as it expects to run during the site generation phase. However, this causes the check goal to run as well, and we didn’t need that. So maybe, I’ll work up a patch to add a reporting goal just to run the report without having to run the site goal as well. Hopefully, this helps some people, as I lost much hair working this out.

Happy coding and don’t forget to share!

Reference: Enable Code Coverage for Integration and Unit Tests using Cobertura and Maven from our JCG partner David Carver at the Intellectual Cramps blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


× eight = 72



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close