19

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?

Panda TG Attwood
  • 1,468
  • 2
  • 16
  • 31

2 Answers2

16

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.

Panda TG Attwood
  • 1,468
  • 2
  • 16
  • 31
  • Which "current" version does it not work with and why? – chrylis -cautiouslyoptimistic- Nov 02 '17 at 15:44
  • 3
    For me failsafe still doesn't pick up the integration tests. surefire doesn that. – Krzysztof Krasoń Nov 30 '17 at 11:12
  • Have the same issue as @KrzysztofKrasoń. My tests are not pick up by failsafe. :( – dadoonet Jan 19 '18 at 18:25
  • @KrzysztofKrasoń @dadoonet I've gone over this and tested it with an example (https://github.com/TickleThePanda/junit5-failsafe-example) and it still works for me. Have you named the Integration Tests correctly (by default Failsafe looks for `**/*IT.java`, `**/IT*.java`, and `**/*ITCase.java` which are also ignored by surefire)? – Panda TG Attwood Jan 22 '18 at 15:55
  • @Thomas'Panda'Attwood If I clone your repo and run `mvn clean install` from the non-faily dir it makes no mention of ExampleIT and also if I edit ExampleIT, changing it to assertTrue(false); it still succeeds. I'm using mvn v3.3.9 is this different for you? – CamW Apr 25 '18 at 15:56
  • 2
    The repo at github.com/TickleThePanda/junit5-failsafe-example is broken and doesn't run the integration tests because there is no executions element in the failsafe plugin. – CamW Apr 26 '18 at 06:50
  • Yeah, I didn't include the executions in there. I've updated the instructions in the `README.md` to that more clear now. (https://github.com/TickleThePanda/junit5-failsafe-example/commit/b4dbea943f2628c164acefbf0caf4aeb8801b7b9) – Panda TG Attwood May 17 '18 at 10:55
14

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>  
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • @Thomas 'Panda' Attwood In fact it is not a workaround but the right way. I found the exact reference. – davidxxx Aug 15 '18 at 14:07
  • 2
    maven-failsafe-plugin version 2.22.0 doesn't support JUnit 5 natively yet. When I upgraded to 2.22.0 and removed the dependencies section, maven ignored all of my JUnit5 integration tests. – Tom Norton Dec 13 '18 at 16:19
  • @Tom Norton I agree. That's why I said that `junit-jupiter-engine` dependency was still required. But the platform dependency is not required any longer. – davidxxx Dec 14 '18 at 09:23
  • 1
    Something along those lines worked for me. Thanks for posting it. I still had issues with failsafe not finding the tests. It turned out to be related to our use of SpringBoot (which is not part of the original question but some may come here for answers). What helped me with that was to change the way the jar is packaged - following suggestions in this bug ticket about the incompatibility: https://github.com/spring-projects/spring-boot/issues/6254 (now I can run failsafe 2.22.2 with SpringBoot 2.2) – Ondrej Burkert Dec 18 '19 at 13:15