咱的Maven项目能用Junit5吗?

1. 简介

不一样于 JUnit3 或者 JUnit4,JUnit 5 由多个模块组成,并三个核心的子项目:html

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintagegit

查看一下官网 go JUnit5,上面有最新版本显示:github

这版本号能够帮助咱们填写下面的依赖 junit-platform-launcher AND junit-jupiter-engine AND junit-vintage-engineapache

<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>

为了 mvn test 顺利执行 Junit5 测试用例,那么你须要 Maven 插件 maven-surefire-plugin:api

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

2. 思考

这三个子项目,咱们每次都须要引入吗?intellij-idea

  • 答案:不是的,实际仍是须要因项目需求而异的。

3. 应用场景

首先项目必须 JDK8+

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

  • 也就是说须要 JDK8+ 的项目才能用 JUnit5,不然老老实实选用 JUnit4 吧。

3.1 新项目用JUnit5

若是,咱们是一个新开的项目,须要使用 Junit5,怎么选?ide

  • 答案:只须要引入 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 迁移至JUnit5

若是,咱们的项目是一个旧项目,用的是 JDK8 + JUnit4,如今新的测试用例想要用 JUnit5 来写,怎么办?测试

  • 答案:引入 junit-jupiter-engine AND junit-vintage-engine。原先的依赖 junit 能够考虑移除,由于 junit-vintage-engine 会为咱们自动引入可传递依赖项 junit
<?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. 解读

要开始使用 JUnit5 Platform,您至少须要向项目中添加一个 TestEngine 实现。ui

junit-jupiter-engine 项目包含实现类 JupiterTestEnginejunit-vintage-engine 项目包含实现类 VintageTestEngine

若是您想用 Jupiter 编写测试,请将测试件 junit-jupiter-engine 添加到 POM 中的依赖项中。这将引入全部必需的依赖项。在这些依赖关系中,就有 junit-jupiter-api,它包含测试源代码编译所需的类和接口。

咱们一般使用的 @Test,@Disabled 等注解,Assertions 等断言 API 都出自 junit-jupiter-api

咱们在项目中引入依赖 junit-jupiter-engine 时,Maven 就会自动替咱们引入 junit-jupiter-api

若是您想经过JUnit平台编写和执行JUnit3或4测试,请将 junit-vintage-engine 添加到依赖项中,该引擎可传递地引入(并须要)junit:junit:4.12

引入依赖 junit-vintage-engine 时,Maven 就会自动替咱们引入 junit

5. 为啥没用到 junit-platform-launcher?

IntelliJ IDEA在IDEA 2017.3 以前发布了 JUnit 5 的特定捆绑包版本。所以,若是您想使用较新版本的Junit Jupiter,IDE中的测试执行可能会因为版本冲突而失败。在这种状况下,请按照下面的说明使用比 IntelliJ IDEA 捆绑的JUnit 5更新的版本。

junit-platform-launcher 更多的是服务于 IDE 的,若是出现了版本冲突引发的测试执行失败,就须要添加,反之我以为不怎么须要呢?

所以,加或者不加这个 junit-platform-launcher 依赖都有必定的合理性。我的没有遇过这类问题,因此暂时不加。

6. 参考文档

相关文章
相关标签/搜索