5

The problem in 1 sentence: "Cobertura does not produce correct code coverage"

Below is my pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>a.b.c</groupId>
    <artifactId>MyProject</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <repositories>
        <repository>
            <id>google-api-services</id>
            <url>http://google-api-client-libraries.appspot.com/mavenrepo</url>
        </repository>
    </repositories>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java-version>1.6</java-version>
        <maven.test.skip.exec>false</maven.test.skip.exec>
        <cobertura-maven-plugin.version>2.5.2</cobertura-maven-plugin.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>com.google.appengine</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>1.7.4</version>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <extensions>false</extensions>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.1.3</version>
                <configuration>
                    <testFailureIgnore>false</testFailureIgnore>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>${cobertura-maven-plugin.version}</version>
                <configuration>
                    <formats>
                        <format>xml</format>
                        <format>html</format>
                    </formats>
                    <check/>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>cobertura</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>${cobertura-maven-plugin.version}</version>
            </plugin>
        </plugins>
    </reporting>
    <dependencies>
       ...
    </dependencies>
</project>

When i attempt to build with this pom, 2 things happen

  1. All tests run 2 times
  2. war is produced
  3. Cobertura report shows 0% coverage

Please help me debug this issue.

James Raitsev
  • 92,517
  • 154
  • 335
  • 470

2 Answers2

3

The first things which i notice is that you are using an extremely old version of the maven-surefire-plugin (2.1.3 which is about 2006!), but the current version is 2.13.

Apart from that you have bound the cobertura-maven-plugin to the package phase with the reporting goal cobertura which is simply wrong.

The best is to simplify your setup first and get it run which means just define the version of the cobertura-maven-plugin in a property and do the setup in reporting area like this:

  <properties>
    <cobertura-maven-plugin.version>2.5.2</cobertura-maven-plugin.version>
  </properties>

and the following into the reporting area:

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

Just test it with this setup and check if the code coverages has been created or not. If not you need to show more of your project (pom, tests etc.).

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • I followed your instructions and got the same result. The only difference is that with 2.13, tests don't run at all. With 2.1.3 all tests run. Very strange. Per your suggestion, i will post the entire pom.xml up above. Again, thank you for looking into it. – James Raitsev Feb 19 '13 at 00:12
  • Also note, that if i don't have any phase at all, cobertura does not run at all – James Raitsev Feb 19 '13 at 00:15
2

First, I would recommend to run mvn clean install cobertura:cobertura when you want your coverage report to be generated. This is not very likely to be something you want to do for every single build (I personally only use Cobertura in Jenkins).

Second, it may seem annoying to have all the tests run twice, but some people think this is more reliable, and so intended behavior. This is because cobertura instruments your bytecode. Therefore, there is a (very little) chance that this messes up with the results of your tests.

But of course, for this double test run is time consuming, this is another reason why you would not run cobertura:cobertura in your standard lifecycle

Samuel EUSTACHI
  • 3,116
  • 19
  • 24