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> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <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> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> <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> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>${junit-platform.version}</version> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>${junit-vintage-engine}</version> </dependency> <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:
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.
source share