前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Junit5系列-什么是Junit5?

Junit5系列-什么是Junit5?

作者头像
洋仔聊编程
发布2019-01-28 16:24:47
2.2K0
发布2019-01-28 16:24:47
举报

Junit5

官网:JUnit5 is the next generation of JUnit.

注意:以下内容绝大部分翻译自官网

目标是为JVM上的开发人员端测试创建一个最新的基础。例如支持了jdk8的lambda表达式,流式处理等。

JUnit 5是JUnit Lambda和它在Indiegogo上的众筹活动的结果。

简介

组成: JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

其中: JUnit Platform JUnit Platform 作为在JVM上启动测试框架的基础。 它还定义了TestEngine API,用于开发在平台上运行的测试框架。 此外,该平台提供了一个控制台启动器,用于从命令行启动平台,并为Gradle和Maven构建插件,以及一个基于JUnit 4的运行器,用于在平台上运行任何TestEngine。

JUnit Jupiter JUnit Jupiter 是新的编程模型和扩展模型的组合,用于在JUnit 5中编写测试和扩展。 Jupiter子项目为运行基于平台的测试提供了一个测试引擎。

JUnit Vintage JUnit Vintage 为在平台上运行基于JUnit 3和JUnit 4的测试提供了一个测试引擎。

JDK 支持

JUnit 5在运行时要求Java 8(或更高)。但是,您仍然可以测试使用JDK的以前版本编译的代码。

Maven 导入

在使用maven项目时,必须要导入下面的三个依赖,其他的依赖我们可以根据自己的需求导入。

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.3.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.3.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.3.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

例如:如果我们需要使用junit5中的参数化测试功能,我们就需要再添加以下依赖:

代码语言:javascript
复制
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.3.2</version>
    <scope>test</scope>
</dependency>

对于其他的组件,都有什么作用呢?我们下面来看一下:

所有组件介绍

JUnit Platform
  • Group IDorg.junit.platform
  • Version1.3.2
  • Artifact IDs
    • junit-platform-commons JUnit的内部公共库/实用程序。这些实用程序仅用于JUnit框架本身。不支持外部方的任何使用。使用风险自负!
    • junit-platform-console 支持从控制台在JUnit平台上发现和执行测试。有关详细信息,请参阅 Console Launcher
    • junit-platform-console-standalone 包含所有依赖项的可执行JAR在junit-platform-console-standalone 目录下的Maven Central中提供 。有关详细信息,请参阅Console Launcher
    • junit-platform-engine 测试引擎的公共API。有关详细信息,请参阅插入自己的测试引擎
    • junit-platform-launcher 用于配置和启动test plans的公共API - 通常由IDE和构建工具使用。有关详细信息,请参阅JUnit Platform Launcher API
    • junit-platform-runner 用于在JUnit 4环境中的JUnit平台上执行测试和测试套件的运行器。 也就是我们在只有Junit4的环境下,我们通过添加此依赖,可以直接使用Junit5中的一些功能。 有关详细信息,请参阅使用JUnit 4运行JUnit平台
    • junit-platform-suite-api 当我们需要进行嵌套测试时,就是该依赖上场的时候来了。由JUnitPlatform runner支持 ,可能由第三方 TestEngine实现支持。
    • junit-platform-surefire-provider 支持使用Maven Surefire在JUnit平台上发现和执行测试 。
JUnit Jupiter
  • Group IDorg.junit.jupiter
  • Version5.3.2
  • 工件ID
    • junit-jupiter-api 用于对 JUnit Jupiter API 的 编写测试扩展
    • junit-jupiter-engine JUnit Jupiter测试引擎实现,仅在运行时需要,也是我们在使用junit5时必须要添加的。
    • junit-jupiter-params 支持JUnit Jupiter中的参数化测试。使用参数化测试的时候就要添加此依赖了。
    • junit-jupiter-migrationsupport 从JUnit 4到JUnit Jupiter的迁移支持,仅在运行选定的JUnit 4规则时才需要。
JUnit Vintage
  • Group IDorg.junit.vintage
  • Version5.3.2
  • 工件ID
    • junit-vintage-engine JUnit Vintage测试引擎实现,允许在新的JUnit平台上运行老的JUnit测试,即以JUnit 3或JUnit 4样式编写的测试。
Junit5 BOM

什么事BOM?

BOM:Bill of Materials材料清单的意思,其定义一整套相互兼容的jar包版本集合,使用时只需要依赖该BOM文件,即可放心的使用需要的依赖jar包,且无需再指定版本号。BOM的维护方负责版本升级,并保证BOM中定义的jar包版本之间的兼容性。

在使用MavenGradle引用多个上述工件时,可以使用以下Maven坐标下提供Bill of Materials POM来简化依赖关系管理 。

  • Group IDorg.junit
  • Artifact IDjunit-bom
  • Version5.3.2

也就是:

代码语言:javascript
复制
<dependency>
    <groupId>org.junit</groupId>
    <artifactId>junit-bom</artifactId>
    <version>5.3.2</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

添加了上述bom依赖后,我们便不需要在添加依赖的时候添加<version>5.3.2</version>版本号了,版本号与版本之间的关系直接有Bom控制。 不过要注意的是:并不是简单的讲上述依赖放在中而是放 下才会起作用

下面我们可以看一下,添加bom后的pom文件部分内容:

代码语言:javascript
复制
<!--此处注意应该放在dependencyManagement中-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.3.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!--可以不添加版本号,而由bom控制其版本依赖-->
<dependencies>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
    </dependency>
</dependencies>

此外,大多数上述依赖对以下OpenTest4J JAR 具有直接或传递依赖性。所以当需要此依赖时,就需要加上了。

  • Group IDorg.opentest4j
  • Artifact IDopentest4j
  • Version1.1.1

也就是:

代码语言:javascript
复制
<dependency>
    <groupId>org.opentest4j</groupId>
    <artifactId>opentest4j</artifactId>
    <version>1.1.1</version>
    <scope>test</scope>
</dependency>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Junit5
  • 简介
  • JDK 支持
  • Maven 导入
  • 所有组件介绍
    • JUnit Platform
      • JUnit Jupiter
        • JUnit Vintage
          • Junit5 BOM
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档