Can our Maven project use Junit5?

1. Introduction

Unlike JUnit 3 or JUnit 4, JUnit 5 consists of multiple modules and three core subprojects:

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

Check the official website go JUnit5 , the latest version shows:

This version number can help us fill in the following dependent JUnit platform launcher and JUnit Jupiter engine and JUnit Vintage engine:

<dependencies>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.7.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

In order for mvn test to successfully execute Junit5 test cases, you need Maven plugin Maven surefire plugin:

<plugins>
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
    </plugin>
</plugins>

2. Thinking

Do we need to introduce these three sub projects every time?

  • Answer: No, the actual needs vary according to the needs of the project.

3. Application scenario

First, the project must be JDK8+

JUnit 5 requires Java 8 (or higher) at runtime.

  • In other words, projects that need JDK8 + can use JUnit 5. Otherwise, choose JUnit 4 honestly.

3.1 JUnit5 for new projects

If we are a new project and need to use JUnit 5, how to choose?

  • Answer: just introduce JUnit Jupiter engine.
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
    <artifactId>junit5-jupiter-starter-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>

        <junit5.version>5.7.1</junit5.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>

</project>

3.2 migrating to JUnit5

If our project is an old project, using JDK8 + JUnit4, and now the new test cases want to be written in JUnit5, what should we do?

  • Answer: introduce junit Jupiter engine and junit Vintage engine. The original dependency junit can be considered to be removed, because junit Vintage engine will automatically introduce the transitive dependency junit for us
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
    <artifactId>junit5-migration-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>

        <junit5.version>5.7.1</junit5.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>

</project>

4. Interpretation

To start using the JUnit 5 platform, you need to add at least one TestEngine implementation to your project.

The JUnit Jupiter engine project contains the implementation class Jupiter testengine, and the JUnit Vintage engine project contains the implementation class Vintage testengine.

If you want to write tests with Jupiter, add the test piece JUnit Jupiter engine to the dependency in POM. This introduces all required dependencies. Among these dependencies, there is the JUnit Jupiter API, which contains the classes and interfaces required for testing source code compilation.

We usually use @ Test, @ Disabled and other annotations, Assertions and other assertion APIs from JUnit Jupiter API.

When we introduce the dependency JUnit Jupiter engine into the project, Maven will automatically introduce JUnit Jupiter API for us.

If you want to write and execute JUnit 3 or 4 tests through the JUnit platform, add JUnit Vintage engine to the dependency, which transitively introduces (and requires) junit:junit:4.12

When dependency junit Vintage engine is introduced, Maven will automatically introduce junit for us

5. Why didn't you use JUnit platform launcher?

IntelliJ IDEA released a specific bundle version of JUnit 5 before IDEA 2017.3. Therefore, if you want to use a newer version of Junit Jupiter, test execution in the IDE may fail due to version conflicts. In this case, please follow the instructions below to use a newer version than JUnit 5 bundled with IntelliJ IDEA.

JUnit platform launcher mostly serves IDE. If there is a test execution failure caused by version conflict, it needs to be added. On the contrary, I don't think it's necessary?

Therefore, it is reasonable to add or not add the JUnit platform launcher dependency. I haven't experienced such problems, so I won't add them for the time being.

6. Reference documents

Tags: Java

Posted by mcccy005 on Fri, 15 Apr 2022 20:38:10 +0930