目录

(九)JUnit5 介绍与安装

官方网址:http://junit.org/junit5/

Junit5 已经不算是新的版本了,2016 年推出非正式版,相比较 JUnit4 安装和使用都有一定的差异。

JUnit5 介绍


The new major version of the programmer-friendly testing framework for Java 8

一个新的重要版本,程序员更友的测试框架,基于 Java8。

关于

JUnit5 是 JUnit 的下一代。我们的目标是为 JVM 上的开发人员端测试创建一个最新的基础。这包括针对 Java 8 及以上,以及使许多不同风格的测试。

Junit5 组成

先看来个公式:

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

这看上去比 Junit4 复杂,实际上在导入包时也会复杂一些。

  • JUnit Platform 是在JVM上启动测试框架的基础。

  • JUnit Jupiter 是JUnit5扩展的新的编程模型和扩展模型,用来编写测试用例。Jupiter子项目为在平台上运行Jupiter的测试提供了一个TestEngine (测试引擎)。

  • JUnit Vintage 提供了一个在平台上运行 JUnit3 和 JUnit4 的 TestEngine 。

Maven 安装


首先,你需要通过 IntelliJ IDEA 创建一个 Maven 项目,IntelliJ IDEA 集成的有 Maven,所以,你很容易做到这一点。通过 Maven 的 pom.xml 文件,添加 Junit5 。

pom.xml 文件配置如下:

<dependencies>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.0.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.0.1</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>4.12.1</version>
        <scope>test</scope>
    </dependency>

</dependencies>

原始封面

https://images.unsplash.com/photo-1581094288338-2314dddb7ece?w=300