Configuration and Execution Strategies for Integration Tests in Maven Multi-module Projects

Nov 22, 2025 · Programming · 13 views · 7.8

Keywords: Maven | Integration Testing | Surefire Plugin

Abstract: This article provides an in-depth exploration of how to properly configure and execute integration tests in Maven multi-module projects. By analyzing the Maven build lifecycle and Surefire plugin configuration methods, it details best practices for separating unit tests from integration tests. The article includes complete XML configuration examples and explains how to manage test execution through different Maven phases and test naming patterns, ensuring integration tests run after proper environment preparation and cleanup.

Maven Build Lifecycle and Test Phases

The Maven build lifecycle defines various phases in the project build process, with test-related phases including test and integration-test. The test phase is specifically designed for executing unit tests, while the integration-test phase is used for running integration tests, which typically execute after the application is packaged to verify integration between different modules.

Surefire Plugin Configuration Strategy

The Maven Surefire plugin is the core tool for test execution. To separate unit tests from integration tests, different execution strategies need to be defined in the plugin configuration. Below is a complete configuration example:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <excludes>
      <exclude>**/*IntegrationTest.java</exclude>
    </excludes>
  </configuration>
  <executions>
    <execution>
      <id>integration-test</id>
      <goals>
        <goal>test</goal>
      </goals>
      <phase>integration-test</phase>
      <configuration>
        <excludes>
          <exclude>none</exclude>
        </excludes>
        <includes>
          <include>**/*IntegrationTest.java</include>
        </includes>
      </configuration>
    </execution>
  </executions>
</plugin>

In this configuration, the default test execution excludes all test classes ending with IntegrationTest.java, ensuring that only unit tests run during the test phase. Simultaneously, a new execution configuration is defined to specifically run integration tests during the integration-test phase.

Test Naming Conventions and Pattern Matching

Proper test class naming is crucial for ensuring configuration effectiveness. Recommended naming patterns for different test types include:

The Maven Surefire plugin uses Ant-style pattern matching to identify test classes. The pattern **/*IntegrationTest.java matches any file ending with IntegrationTest.java in any package path.

Execution Commands and Lifecycle Integration

To run integration tests, use the mvn verify command instead of directly invoking the integration-test phase. This is because the verify phase ensures necessary environment cleanup operations are executed after integration tests complete.

When executing mvn test, only unit tests are executed. When executing mvn verify, Maven sequentially executes:

  1. Compilation and packaging phases
  2. Unit test phase
  3. Integration test preparation phase
  4. Integration test execution phase
  5. Integration test cleanup phase
  6. Verification phase

Configuration Considerations in Multi-module Projects

In multi-module projects, integration test configuration needs to be defined in each submodule containing integration tests. The parent POM can define plugin management to ensure all submodules use consistent configurations.

If using the Failsafe plugin as an alternative, it is specifically designed for integration tests and provides a safer test execution mechanism. The Failsafe plugin does not immediately terminate the build upon test failure, allowing subsequent cleanup operations to execute normally.

Common Issues and Solutions

In practice, developers may encounter issues where integration tests are not executed. Common causes include:

Most execution issues can be resolved by carefully checking configurations and test class naming. It is recommended to use mvn clean verify during development to ensure all tests execute correctly.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.