Running JUnit 4 and JUnit 5 tests in the same assembly

In Maven projects, I have some existing tests based on JUnit 4. I cannot port these tests to JUnit 5 for several reasons.
In fact, some tests depend on the library that uses the JUnit 4 runner, and code migration may take some time.

I would still like to create new test classes with JUnit 5, which is now released and provides interesting new features.
How to do it?

+5
source share
1 answer

JUnit 5 provides the way out of the box .

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

Each of them is a separate project, and using all of them allows you to compile and run JUnit 4 and JUnit 5 tests in one project.

JUnit Jupiter is a combination of the new programming model and extension model for writing tests and extensions in JUnit 5.

JUnit Vintage provides TestEngine to run tests based on JUnit 3 and JUnit 4. on the platform.

The JUnit platform serves as the basis for running test frameworks on the JVM

The following is the minimum configuration used with Maven to set up a project to compile and run JUnit4 and JUnit5 tests:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>mygroup</groupId> <artifactId>minimal-conf-junit4-5</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <!-- JUnit 5 depends on JDK 1.8 --> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!-- JUnit dependency versions --> <junit.version>4.12</junit.version> <junit-vintage-engine>4.12.1</junit-vintage-engine> <junit-jupiter.version>5.0.1</junit-jupiter.version> <junit-platform.version>1.0.1</junit-platform.version> </properties> <dependencies> <!--JUnit Jupiter API to write and compile tests with JUnit5 --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> <!-- JUnit 4 to make legacy JUnit 4 tests compile --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <!-- matters until now--> <dependencies> <!-- to let surefire to run JUnit 4 but also JUnit 5 tests --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>${junit-platform.version}</version> </dependency> <!-- JUnit vintage engine to run JUnit 3 or JUnit 4 tests --> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>${junit-vintage-engine}</version> </dependency> <!-- JUnit 5 engine to run JUnit 5 tests --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit-jupiter.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> </project> 

Now mvn test compiles and runs JUnit 4 and JUnit 5 tests.

Note 1: The dependencies junit-vintage-engine ( 4.12.1 ) and junit ( 4.12 ) do not indicate the same exact version.
This is not a problem at all:

  • their release is not connected between them

  • junit-vintage-engine designed to run any JUnit 3 or 4 test.

Note 2: maven-surefire-plugin with version 2.19.1 has a value that you want to use only JUnit5 or JUnit4 and JUnit5.
The next version of the plugin does indeed throw some exceptions during the execution of JUnit 5 tests.

The problem is quite old, but not yet resolved.

+9
source

Source: https://habr.com/ru/post/1273167/


All Articles