How to Separate Unit and Integration Tests in Maven and Visualize Coverage with SonarQube
This guide explains how to restructure a Maven project to isolate unit and integration tests, configure Maven Surefire, Failsafe, Build‑Helper, and JaCoCo plugins for separate execution and combined coverage reports, and finally display the results in SonarQube.
Project structure for separating unit and integration tests
Modify the default Maven layout by adding a dedicated source directory for integration tests ( src/it/java) while keeping the standard unit‑test directory ( src/test/java). The resulting structure is:
FunTester-app
├── pom.xml
├── src
│ ├── it
│ │ └── java
│ ├── main
│ │ └── java
│ └── test
│ └── javaRunning unit tests
Unit tests are executed automatically by Maven’s default lifecycle. They must reside in src/test/java and their class names should start or end with Test or TestCase. Maven runs them during the test phase.
Running integration tests
Integration tests are placed in src/it/java. Their class names should start with IT or end with IT or ITCase. By default Maven does not execute them; they require additional plugin configuration.
Adding the integration‑test source directory
Use build-helper-maven-plugin to add src/it/java (and optional resources) 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>Insert this fragment into the <project><build><plugins> section of the root pom.xml.
Configuring the Failsafe plugin for integration tests
Bind the Maven Failsafe plugin to the integration-test and verify goals so that tests in src/it/java run automatically during the integration‑test phase:
<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>Generating a combined coverage report with JaCoCo
Configure jacoco-maven-plugin to prepare agents for both unit and integration test runs and to produce separate reports that can be merged for SonarQube:
<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>This configuration ensures that coverage data from unit and integration tests are collected and merged into a single report that SonarQube can consume.
SonarQube visualization
After a successful build, run the Sonar Maven scanner ( mvn sonar:sonar) using the sonar-maven-plugin. Add a Sonar profile to ~/.m2/settings.xml so the scanner knows where to upload the report:
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>http://localhost:8080</sonar.host.url>
</properties>
</profile>When the project is opened in the SonarQube dashboard, the overall coverage metric will reflect the combined results of both unit and integration tests.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
