Search code examples
javamavenmaven-plugincoberturamaven-cobertura-plugin

Cobertura code coverage test on Maven plugin


I'm trying to generate a code coverage report for a simple Maven plugin I've developed. Cobertura generates the report with the three classes in my project correctly, but it reports 0% code coverage even though the tests execute successfully. I've run it in debug mode and there are no errors or stack traces reported by Cobertura.

My configuration in the POM file is quite simple:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-artifact</artifactId>
        <version>3.0.5</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-compat</artifactId>
        <version>3.0.5</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.0.5</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
    </dependency> 

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-core</artifactId>
        <version>3.0.5</version>
    </dependency>  

    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <!-- version 2.1 uses sonatype aether. anything after 2.1 uses eclipse aether. -->
        <groupId>org.apache.maven.plugin-testing</groupId>
        <artifactId>maven-plugin-testing-harness</artifactId>
        <scope>test</scope>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.twdata.maven</groupId>
        <artifactId>mojo-executor-maven-plugin</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <type>maven-plugin</type>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <executions>
                <execution>
                    <id>test-custom-plugin</id>
                    <phase>test</phase>
                    <goals> 
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <forkMode>never</forkMode>
                        <forkCount>0</forkCount>
                        <reuseForks>true</reuseForks>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <goalPrefix>MyCustomPlugin</goalPrefix>
            </configuration>
            <executions>
                <execution>
                    <id>default-descriptor</id>
                    <goals>
                        <goal>descriptor</goal>
                    </goals>
                    <phase>process-classes</phase>
                </execution>
                <execution>
                    <id>help-descriptor</id>
                    <goals>
                        <goal>helpmojo</goal>
                    </goals>
                    <phase>process-classes</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.5.2</version>
        </plugin>
    </plugins>
</build>    

Cobertura works on all of my other projects (so far), is there any reason that it would failing to report coverage for a Maven plugin project?


Solution

  • It seems the problem was with my surefire configuration. I changed it to

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
                <executions>
                    <execution>
                        <id>test-custom-plugin</id>
                        <phase>test</phase>
                        <goals> 
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <forkCount>1</forkCount>
                            <reuseForks>true</reuseForks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    and reports are now generated correctly. I got the idea for changing the fork options from a number of posts I'd seen of similar problems related to generating reports for sonar plugins, and changing the fork options resolved the issue.