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.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


+ 9 = eleven



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books