Fundamentals 11 min read

Configuring JaCoCo Maven Plugin for Separate Unit and Integration Test Coverage Reports

This article explains how to configure the JaCoCo Maven plugin to generate separate code coverage reports for unit and integration tests, including plugin setup in the POM, execution configurations, and integration with Maven Surefire and Failsafe plugins, plus Maven commands to produce the reports.

FunTester
FunTester
FunTester
Configuring JaCoCo Maven Plugin for Separate Unit and Integration Test Coverage Reports

This blog post explains how to configure the JaCoCo Maven plugin to generate separate code coverage reports for unit tests and integration tests, ensuring each report is written to its own directory.

Configuring the JaCoCo Maven Plugin

The plugin provides access to the JaCoCo runtime agent that records execution data and creates coverage reports based on that data.

To add the plugin, insert the following snippet into the <plugins> section of the POM:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.3.201306030806</version>
</plugin>

Configuring Unit Test Coverage

Two executions are required: one to prepare the JaCoCo agent (property surefireArgLine ) and another to generate the report after tests run.

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.3.201306030806</version>
    <executions>
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

Configuring Integration Test Coverage

Similarly, two executions are added for integration tests, using the property failsafeArgLine and writing data to jacoco-it.exec .

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.3.201306030806</version>
    <executions>
        <execution>
            <id>pre-integration-test</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
                <propertyName>failsafeArgLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>post-integration-test</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

Configuring Maven Surefire Plugin

The Surefire plugin runs unit tests; its argLine is set to ${surefireArgLine} to activate the JaCoCo agent.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
        <argLine>${surefireArgLine}</argLine>
        <skipTests>${skip.unit.tests}</skipTests>
        <excludes>
            <exclude>**/IT*.java</exclude>
        </excludes>
    </configuration>
</plugin>

Configuring Maven Failsafe Plugin

The Failsafe plugin runs integration tests; its argLine is set to ${failsafeArgLine} .

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.15</version>
    <executions>
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <argLine>${failsafeArgLine}</argLine>
                <skipTests>${skip.integration.tests}</skipTests>
            </configuration>
        </execution>
    </executions>
</plugin>

Generating the Coverage Reports

With the plugins configured, the following Maven commands produce the reports:

mvn clean test – runs unit tests and creates a report in target/site/jacoco-ut .

mvn clean verify -P integration-test – runs integration tests and creates a report in target/site/jacoco-it .

mvn clean verify -P all-tests – runs both test types and generates both reports.

Code CoverageMavenunit testingintegration-testingJaCoCo
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.