How to Get My Maven Integration Tests to Run

How do I get my Maven Integration tests to run

You can set up Maven's Surefire to run unit tests and integration tests separately. In the standard unit test phase you run everything that does not pattern match an integration test. You then create a second test phase that runs just the integration tests.

Here is an 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>

How to make integration tests and unit tests run separately through maven?

Maven has a build lifecycle made up out of several phases. When you call a particular one, all phases before that one will be executed first.
See https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

There are two ways how you could solve what you want:

  • use -DskipTests or -Dmaven.test.skip=true (https://www.mkyong.com/maven/how-to-skip-maven-unit-test/)
  • call the plugin goal directly mvn clean test-compile failsafe:integration-test

Prevent unit tests but allow integration tests in Maven

I found the simplest way to skip only surefire tests is to configure surefire (but not failsafe) as follows:

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<configuration>
<!-- skips surefire tests without skipping failsafe tests.
Property value seems to magically default to false -->
<skipTests>${skip.surefire.tests}</skipTests>
</configuration>
</plugin>

This allows you to run mvn verify -Dskip.surefire.tests and only surefire, not failsafe, tests will be skipped; it will also run all other necessary phases including pre-integration and post-integration, and will also run the verify goal which is required to actually fail your maven build if your integration tests fail.

Note that this redefines the property used to specify that tests should be skipped, so if you supply the canonical -DskipTests=true, surefire will ignore it but failsafe will respect it, which may be unexpected, especially if you have existing builds/users specifying that flag already. A simple workaround seems to be to default skip.surefire.tests to the value of skipTests in your <properties> section of the pom:

<properties>
<skip.surefire.tests>${skipTests}</skip.surefire.tests>
</properties>

If you need to, you could provide an analagous parameter called skip.failsafe.tests for failsafe, however I haven't found it necessary - because unit tests usually run in an earlier phase, and if I want to run unit tests but not integration tests, I would run the test phase instead of the verify phase. Your experiences may vary!

These skip.(surefire|failsafe).tests properties should probably be integrated into surefire/failsafe code itself, but I'm not sure how much it would violate the "they're exactly the same plugin except for 1 tiny difference" ethos.

how to run integration test-case for maven project

You can do like this

<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>

How can I run integration tests after building all modules in a multi-module Maven project?

Here's a solution that does exactly what I want: using the Failsafe plugin to run just the integration tests without having to move tests out of their natural places or recompile in order to run the tests.

mvn install -DskipITs
mvn failsafe:integration-test

We get Maven to publish the artifacts for the project into a local directory. We can then use that as a repository for the second step. In that second step we then invoke exactly the goal that we need and it grabs the artifacts from the local repository.

The same thing works for Surefire (mvn surefire:test) if you need to run unit tests separately.

I have to admit that I don't fully understand the Maven model and hence why this works. The internet seems unanimously of the view that this kind of thing can't be done (see other answers here), but it's definitely working for me.



Related Topics



Leave a reply



Submit