Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run JUnit 5 integration tests with the Maven Failsafe plugin?

The Maven Failsafe plugin won't find my JUnit 5 integration tests when I'm running the command mvn clean failsafe:integration-test, although it can find the files.

I have the junit-jupiter-api and junit-jupiter-engine as test dependencies:

<properties>
    <junit.jupiter.version>5.0.1</junit.jupiter.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

My integration tests are named correctly (following the **/*IT.java, **/IT*.java, or the **/*ITCase.java that included by default by Failsafe and excluded by default by Surefire).

Is there any way that I can use JUnit 5 tests with Failsafe?

like image 700
Panda TG Attwood Avatar asked Nov 02 '17 15:11

Panda TG Attwood


People also ask

How do I run a JUnit 5 test in Maven?

The Maven Surefire Plugin 2.22. 0 (or newer) provides native support for JUnit 5. If we want to use the native JUnit 5 support of the Maven Surefire Plugin, we must ensure that at least one test engine implementation is found from the classpath. We can run our unit tests by using the command: mvn clean test.

How do I run a specific integration-test in Maven?

The simplest way to run integration tests is to use the Maven failsafe plugin. By default, the Maven surefire plugin executes unit tests during the test phase, while the failsafe plugin runs integration tests in the integration-test phase.

How does maven failsafe plugin work?

The Failsafe Plugin is used during the integration-test and verify phases of the build lifecycle to execute the integration tests of an application. The Failsafe Plugin will not fail the build during the integration-test phase, thus enabling the post-integration-test phase to execute.

What is the difference between maven surefire plugin and Maven failsafe plugin?

maven-surefire-plugin is designed for running unit tests and if any of the tests fail then it will fail the build immediately. maven-failsafe-plugin is designed for running integration tests, and decouples failing the build if there are test failures from actually running the tests.


2 Answers

Edit: This answer was correct before maven-failsafe-plugin:2.22.0. See davidxxx's answer for the ideal and most up to date solution.


The maven-failsafe-plugin currently doesn't support JUnit 5, out of the box.

However, like with maven-surefire-plugin, you can run JUnit 5 tests with the maven-failsafe-plugin by specifying the dependency on the org.junit.platform:junit-platform-surefire-provider:1.0.1 with the earlier version of the maven-failsafe-plugin:2.19.1.

It doesn't work with the current version 2.20 of the failsafe (in the same way that the surefire has the error) due to an OutOfMemory error.

See the below for an example of the configuration of the plugin:

<properties>
    <junit.platform.version>1.0.1</junit.platform.version>
</properties>

<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>${junit.platform.version}</version>
        </dependency>
    </dependencies>
</plugin>

You can find a full example of this working (and a failing one) on GitHub. To test that it works, you can run mvn clean failsafe:integration-test.

like image 163
Panda TG Attwood Avatar answered Oct 12 '22 00:10

Panda TG Attwood


Note that from the JUnit 5 documentation : junit-platform-surefire-provider should be not used any longer :

Due to the release of Surefire 2.22.0, the junit-platform-surefire-provider from the JUnit team has been deprecated and will be discontinued in a subsequent release of the JUnit Platform.

Additionally, you can also read in the maven-surefire-plugin documentation :

Using JUnit 5 Platform

To get started with JUnit Platform, you need to add at least a single TestEngine implementation to your project. For example, if you want to write tests with Jupiter, add the test artifact junit-jupiter-engine to the dependencies in POM

So you have to specify this test dependency :

<properties>
    <junit-jupiter.version>5.2.0</junit-jupiter.version>
</properties> 

<dependencies>
     [...]
     <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-engine</artifactId>
         <version>${junit-jupiter.version}</version>
         <scope>test</scope>
     </dependency>
     [...] 
</dependencies>

And the maven-failsafe-plugin declaration could be as simple as :

<build>
    <plugins>           
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.0</version>
        </plugin>
    </plugins>
</build>  
like image 31
davidxxx Avatar answered Oct 12 '22 00:10

davidxxx