Monday, 6 August 2012

Code Coverage with Maven (One Pass)

Probably not the only solution to generate a code coverage HMTL report during the maven build process, but the Emma plugin does the job. In one pass. The code is available from Github in the Code-Coverage-One-Pass directory.

Assuming the following class:
public class ClassToTest {

    public static int coveredMethod(int i) {
        return i + 2;
    }

    public static int uncoveredMethod(int i) {
        return i + 3;
    }

}
with the following JUnit test:
public class ClassToTestTest {

    public ClassToTestTest() {
    }

    @Test
    public void testCoveredMethod() {
        assertEquals(4,ClassToTest.coveredMethod(2));
    }

}
one can configure the pom.xml as following:
    <build>

        <plugins>

            <plugin>
                <groupId>org.sonatype.maven.plugin</groupId>
                <artifactId>emma-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
                <configuration>
                    <classesDirectory>${project.build.directory}/generated-classes/emma/classes</classesDirectory>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.sonatype.maven.plugin</groupId>
                <artifactId>emma4it-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>instrument</id>
                        <goals>
                            <goal>instrument-project-artifact</goal>
                        </goals>
                        <configuration>
                            <appendEmma>true</appendEmma>
                        </configuration>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                        <sourceSets>
                            <sourceSet>
                            <directory>${project.build.sourceDirectory}</directory>
                            </sourceSet>
                        </sourceSets>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>

    <dependencies>

        <dependency>
            <groupId>org.sonatype.maven.plugin</groupId>
            <artifactId>emma-maven-plugin</artifactId>
            <version>1.2</version>
        </dependency>

        ...

    </dependencies>

It will produce an index.html file in /target/site/emma/ (always the same location, so you can keep you browser open and refresh the after each build process):

No comments:

Post a Comment