Explain Codes LogoExplain Codes Logo

How do I get my Maven Integration tests to run

java
maven-integration-tests
test-phases
surefire-plugin
Anton ShumikhinbyAnton Shumikhin·Feb 12, 2025
TLDR

Kick off your Maven Integration tests with the maven-failsafe-plugin. Incorporate the following snippet in your pom.xml:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.22.2</version> <!-- Always wear the latest version, you'll look cooler --> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin>

Fire up the command mvn verify to run your integration tests. Make sure they follow the naming convention **/*IT.java, to be spotted and executed by the plugin.

Non-negotiable naming for integration tests

Maven Failsafe Plugin automatically recognizes your test files, provided they go by certain names:

  • **/IT*.java
  • **/*IT.java
  • **/*ITCase.java

So, make sure your integration test files follow these rules like the charm. For example, CustomerServiceIT.java or OrderManagementITCase.java would be caught swiftly.

When should the magic begin - triggering integration tests

Unleash the power of integration tests during the subsequent stages of the build lifecycle:

  • mvn verify: Triggers both unit and integration tests.
  • mvn install: The verify phase is included, triggering integration tests.
  • mvn deploy: Same as install, with integration tests on the run.

A line between unit and integration tests

Integration tests tend to hog more time and resources due to involvement with databases, networks and file systems. To demarcate these from zippier unit tests, configure the Maven Surefire Plugin to chuck out integration test cases during the test phase:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludes> <exclude>**/*IT.java</exclude> <!-- Oh no! It doesn't belong here --> <exclude>**/*ITCase.java</exclude> </excludes> </configuration> </plugin>

To single out specific tests:

mvn test -Dtest=MyUnitTest mvn failsafe:integration-test -Dit.test=MyIntegrationTest

Craft your own test phases

In certain scenarios, you might need additional Surefire Plugin executions to manage special cases or to categorize tests differently:

<execution> <id>integration-test</id> <goals> <goal>test</goal> </goals> <configuration> <includes> <include>**/*IntegrationTest.java</include> <!-- You belong here --> </includes> </configuration> <phase>integration-test</phase> <!-- Oh look, a new phase --> </execution>

Integrating JUnit categories

With JUnit, the @Category annotation lets you categorize tests. Configure Maven Surefire to exclude these while Maven Failsafe liberally includes them:

<!-- Surefire configuration --> <excludes> <exclude>**/*IntegrationTest.java</exclude> <!-- Say no to integration tests --> </excludes> <!-- Failsafe configuration --> <includes> <include>**/*IntegrationTest.java</include> <!-- Welcoming integration tests with open arms --> </includes>

Focused plugins for precise testing

Ensure you remember:

  • Surefire Plugin: For unit tests. Trigger with mvn clean test.
  • Failsafe Plugin: For integration tests. Ignite with mvn clean verify.

Skipping tests

  • To dodge unit tests: use mvn install -DskipUnitTests
  • To evade integration tests: try mvn install -DskipIntegrationTests

Troubleshooting idle tests

Your integration tests refuse to budge? Let's diagnose some common speedbumps:

Naming in disarray

Inspect your test file names. It's crucial to adhere to the Failsafe conventions or to customize them in your pom.xml.

Accidently skipping tests

Ensure no properties are set that unintentionally skip tests (look out for -Dmaven.test.skip=true).

Misconfigured plugin

Check your pom.xml for correct plugin configuration. Make sure the goals are accurately defined and the Failsafe Plugin version is current and happening.

Test dependencies hogging the scope

Double-check that test-scoped dependencies aren't meddling with your integration tests.

Encountering unique cases in continuous integration

Let's spotlight some rare yet vital scenarios in integration testing:

Tests in profile

Use Maven profiles for environment-centric configurations:

<profiles> <profile> <id>ci-server</id> <!-- Here's CI server's own space --> <build> <plugins> ... </plugins> </build> </profile> </profiles>

Parallel test execution

Boost your integration tests by rallying them in parallel:

<configuration> <parallel>classes</parallel> <!-- Look, there are few volunteers to run together --> <threadCount>4</threadCount> <!-- Don't worry, there's enough room for everyone! --> </configuration>

Taming the tantrums of flaky tests

Investigate those ephemeral failures. It's wise to consider retries or to tag tests as non-critical if they act up.