Fundamentals 10 min read

Separating Unit and Integration Tests in Maven and Generating Combined JaCoCo Reports for SonarQube

This guide explains how to separate unit and integration tests in a Maven project, configure the Build Helper and Failsafe plugins, set up JaCoCo to generate combined coverage reports, and visualize them in SonarQube, including a complete example pom.xml.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Separating Unit and Integration Tests in Maven and Generating Combined JaCoCo Reports for SonarQube

This article describes how to generate test reports for Maven builds when unit tests and integration tests are run separately, and how to visualize the combined coverage in SonarQube.

Requirements: use Maven as the build tool; the project may be multi‑module (micro‑services); each module contains both unit and integration tests; coverage is measured with the JaCoCo Maven plugin.

Maven Project Structure

Default single‑module layout:

my-app
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   └── test
│       └── java

To separate tests, a new src/it/java directory is added for integration tests, resulting in:

my-app
├── pom.xml
├── src
│   ├── it
│   │   └── java
│   ├── main
│   │   └── java
│   └── test
│       └── java

Unit and Integration Test Execution

Unit tests run automatically when src/test/java exists and test class names follow the *Test or *TestCase pattern; Maven executes them during the test phase of the build lifecycle.

Integration tests require manual configuration. They must reside in src/it/java , have class names starting or ending with IT or ITCase , and run during Maven’s integration-test phase.

The Build Helper Maven Plugin adds src/it/java to the test classpath:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <goals>
                <goal>add-test-source</goal>
                <goal>add-test-resource</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/it/java</source>
                </sources>
                <resources>
                    <resource>
                        <directory>src/it/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

The Maven Failsafe Plugin binds integration tests to the appropriate lifecycle phases:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.0.0-M4</version>
    <configuration>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Test Report Generation

JaCoCo Maven Plugin is configured with two agents (for unit and integration tests) and four goals to prepare agents and generate reports:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.5</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
                <goal>prepare-agent-integration</goal>
                <goal>report</goal>
                <goal>report-integration</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Running mvn verify builds the project, executes both test sets, and produces separate coverage reports that SonarQube can consume.

SonarQube Test Report Visualization

After a successful build, the Sonar Maven plugin uploads the reports to SonarQube. The Sonar server URL is configured in ~/.m2/settings.xml :

<profile>
  <id>sonar</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <properties>
    <!-- Optional URL to server. Default value is http://localhost:9000 -->
    <sonar.host.url>http://localhost:9000</sonar.host.url>
  </properties>
</profile>

When the project is opened in the SonarQube dashboard, the overall test coverage from both unit and integration tests is displayed.

Reference pom.xml

A complete example pom.xml that includes the compiler plugin, Build Helper, Surefire, Failsafe, JaCoCo, and Sonar plugins is provided in the source.

TestingMavenbuildCISonarQubeJaCoCo
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

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.