Configuring Separate Unit and Integration Tests in Maven with JaCoCo and SonarQube Reporting
This guide explains how to structure a Maven project to run unit tests and integration tests separately, configure the Build Helper, Surefire, Failsafe, and JaCoCo plugins, and generate combined test reports that can be visualized in SonarQube.
This article describes how to generate test reports for Maven builds when unit tests and integration tests are run separately, enabling SonarQube to merge them into a comprehensive coverage report.
Requirements:
Use Maven as the build tool.
The project may be a multi‑module (microservice) project.
Unit tests and integration tests are part of each module.
Test coverage is measured with the JaCoCo Maven plugin.
The default Maven project structure for a single‑module project is:
<span>my-app</span>
<span>├── pom.xml</span>
<span>├── src</span>
<span>│ ├── main</span>
<span>│ │ └── java</span>
<span>│ └── test</span>
<span>│ └── java</span>To separate unit and integration tests, add a new directory src/it/java for integration tests, resulting in:
<span>my-app</span>
<span>├── pom.xml</span>
<span>├── src</span>
<span>│ ├── it</span>
<span>│ │ └── java</span>
<span>│ ├── main</span>
<span>│ │ └── java</span>
<span>│ └── test</span>
<span>│ └── java</span>Running Unit Tests
Maven automatically runs unit tests located in src/test/java when the test classes follow the naming convention (start with Test or end with Test / TestCase) during the test phase of the build lifecycle.
Running Integration Tests
Integration tests are placed in src/it/java and must be named with an IT prefix or IT / ITCase suffix. They are executed in the integration-test phase using the Maven Failsafe plugin. The Build Helper Maven plugin adds the src/it/java directory to the test classpath:
<span><plugin></span>
<span> <groupId>org.codehaus.mojo</groupId></span>
<span> <artifactId>build-helper-maven-plugin</artifactId></span>
<span> <version>3.1.0</version></span>
<span> <executions></span>
<span> <execution></span>
<span> <goals></span>
<span> <goal>add-test-source</goal></span>
<span> <goal>add-test-resource</goal></span>
<span> </goals></span>
<span> <configuration></span>
<span> <sources>
<span> <source>src/it/java</source>
<span> </sources>
<span> <resources>
<span> <resource>
<span> <directory>src/it/resources</directory>
<span> </resource>
<span> </resources>
<span> </configuration>
<span> </execution>
<span> </executions>
<span></plugin></span>The Failsafe plugin configuration to bind integration tests to the appropriate lifecycle phases:
<span><plugin></span>
<span> <groupId>org.apache.maven.plugins</groupId></span>
<span> <artifactId>maven-failsafe-plugin</artifactId></span>
<span> <version>3.0.0-M4</version></span>
<span> <configuration>
<span> <encoding>${project.build.sourceEncoding}</encoding>
<span> </configuration>
<span> <executions>
<span> <execution>
<span> <goals>
<span> <goal>integration-test</goal>
<span> <goal>verify</goal>
<span> </goals>
<span> </execution>
<span> </executions>
<span></plugin></span>Generating Test Reports with JaCoCo
To produce separate coverage reports for unit and integration tests, configure the JaCoCo Maven plugin with both prepare-agent and prepare-agent-integration goals, and add report and report-integration goals:
<span><plugin></span>
<span> <groupId>org.jacoco</groupId></span>
<span> <artifactId>jacoco-maven-plugin</artifactId></span>
<span> <version>0.8.5</version></span>
<span> <executions>
<span> <execution>
<span> <goals>
<span> <goal>prepare-agent</goal>
<span> <goal>prepare-agent-integration</goal>
<span> <goal>report</goal>
<span> <goal>report-integration</goal>
<span> </goals>
<span> </execution>
<span> </executions>
<span></plugin></span>After building the project (e.g., mvn verify), both unit and integration test reports are generated.
Visualizing Reports in SonarQube
Run the Sonar Maven plugin after a successful build to upload the reports: mvn sonar:sonar Configure the SonarQube server URL in ~/.m2/settings.xml:
<span><profile></span>
<span> <id>sonar</id></span>
<span> <activation>
<span> <activeByDefault>true</activeByDefault>
<span> </activation></span>
<span> <properties>
<span> <!-- Optional URL to server. Default value is http://localhost:9000 --></span>
<span> <sonar.host.url>http://localhost:9000</sonar.host.url>
<span> </properties>
<span></profile></span>When the project is opened in the SonarQube dashboard, the overall test coverage report, combining unit and integration results, is displayed.
The article concludes with a full example pom.xml that incorporates all the plugins and configurations described above.
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.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
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.
