samaxes

samaxes logo

Ramblings about Open Source, Java and other Web technologies by Samuel Santos

Maven 2 Cobertura Plugin

The Maven 2 Cobertura Plugin web site lacks information to successfully generate Cobertura reports. Worse, some of the usage examples are incorrect and don’t work.

The most common problem when generating Cobertura reports is when the generated report shows 100% test coverage while in reality many of the classes don’t even have tests.

The following example shows how to configure the reports so that it would reflect real test coverage and then check if the specified packages achieved the wanted test coverage:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <check>
                    <haltOnFailure>false</haltOnFailure>
                    <regexes>
                        <regex>
                            <pattern>com.samaxes.business.*</pattern>
                            <branchRate>90</branchRate>
                            <lineRate>90</lineRate>
                        </regex>
                        <regex>
                            <pattern>com.samaxes.persistence.*</pattern>
                            <branchRate>90</branchRate>
                            <lineRate>90</lineRate>
                        </regex>
                    </regexes>
                </check>
                <instrumentation>
                    <includes>
                        <include>com/samaxes/**/*.class</include>
                    </includes>
                </instrumentation>
            </configuration>
            <executions>
                <execution>
                    <id>clean</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>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
        </plugin>
    </plugins>
</reporting>
Bookmark and Share

Related Posts

6 Responses

  1. Birger says:

    Thanks, this should definitely have been on the Maven 2 Cobertura plugin site.

  2. Birger says:

    I had to use version 2.0 to make this work (see http://jira.codehaus.org/browse/MCOBERTURA-57).

  3. Ajay Gupta says:

    I am confused about the snippet above. If you are running the instrumentation goal in Cobertura during the site phase, I am assuming that the test cases have already run by then. So, how would your Cobertura reports show coverage then? Don’t you need to do the instrumentation somewhere between compile and test phases. Please clarify for me.

  4. During the site phase Maven runs the compile and test goals so the code is correctly instrumentated.
    But the code snippet above is already deprecated since the Maven Cobertura plugin has this bug fixed in current versions.

  5. Morten Lauritsen says:

    In which version of the plugin is this bug fixed?

    I am experiencing some funny behaviour (0% coverage despite presence of passing tests), I’d like to exclude the possibility that this is it. Am using v. 2.2 of the plugin.

    Cheers,
    Morten

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Sponsors