In one of my recent backend projects built with Spring Boot, I needed to set up a robust testing strategy that included both unit and integration tests. I also wanted a clear view of my overall code coverage, regardless of whether the tests were unit or integration ones.
This article explains how I configured Maven with the failsafe
plugin for integration testing and how I merged both unit and integration test coverage reports using jacoco-maven-plugin
.
Why Use Failsafe?
By default, Maven’s surefire
plugin runs all test classes during the test
phase. However, this includes only unit tests. If you run integration tests here, they might fail due to the app not being fully initialized or due to side effects.
The failsafe
plugin is designed specifically to run integration tests separately during the integration-test
and verify
phases, after the application has been properly built and started.
Project Setup
Here’s a simplified overview of the relevant sections in my pom.xml
.
Failsafe Plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M5</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
This plugin ensures that only test classes following the naming convention *IT.java
or *ITCase.java
are executed during the integration phase.
JaCoCo Plugin with Merged Reports
I used JaCoCo to generate coverage reports for both unit and integration tests, and then merged them into a single report:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.9</version>
<executions>
<!-- For unit tests -->
<execution>
<id>pre-unit-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<!-- For integration tests -->
<execution>
<id>pre-integration-tests</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>post-integration-tests</id>
<phase>post-integration-test</phase>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
<!-- Merging reports -->
<execution>
<id>merge</id>
<phase>verify</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<fileSets>
<fileSet implementation="org.apache.maven.shared.model.fileset.FileSet">
<directory>${project.basedir}</directory>
<includes>
<include>**/*.exec</include>
</includes>
</fileSet>
</fileSets>
<destFile>target/jacoco/allTest.exec</destFile>
</configuration>
</execution>
<!-- Generate final report -->
<execution>
<id>post-merge-report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/jacoco/allTest.exec</dataFile>
<outputDirectory>target/jacoco/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
This setup generates separate reports for unit and integration tests, merges the .exec
data files, and creates a final report at the end of the build in target/jacoco
.
How to Run the Tests
To execute both types of tests and generate the final coverage report, run:
mvn clean verify
- Unit tests are run during the
test
phase. - Integration tests are executed in
integration-test
and verified inverify
. - The final coverage report is available in
target/jacoco/index.html
.
Conclusion
Using Failsafe for integration tests allows you to maintain clean separation between unit and integration tests. Combining it with JaCoCo gives you an accurate view of your test coverage across all types of tests.
This setup has been essential in ensuring my backend remains well-tested and reliable. If you’re building Spring Boot applications with Maven, I highly recommend giving this configuration a try.
Let me know in the comments how you manage your testing strategy or if you have any tips to improve this setup.
Top comments (0)