官网: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的测试提供了一个测试引擎。
JUnit 5在运行时要求Java 8(或更高)。但是,您仍然可以测试使用JDK的以前版本编译的代码。
在使用maven项目时,必须要导入下面的三个依赖,其他的依赖我们可以根据自己的需求导入。
<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中的参数化测试功能,我们就需要再添加以下依赖:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
对于其他的组件,都有什么作用呢?我们下面来看一下:
org.junit.platform
1.3.2
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平台上发现和执行测试 。
org.junit.jupiter
5.3.2
org.junit.vintage
5.3.2
junit-vintage-engine
JUnit Vintage测试引擎实现,允许在新的JUnit平台上运行老的JUnit测试,即以JUnit 3或JUnit 4样式编写的测试。
什么事BOM?
BOM:
Bill of Materials
材料清单的意思,其定义一整套相互兼容的jar包版本集合,使用时只需要依赖该BOM文件,即可放心的使用需要的依赖jar包,且无需再指定版本号。BOM的维护方负责版本升级,并保证BOM中定义的jar包版本之间的兼容性。
在使用Maven 或Gradle引用多个上述工件时,可以使用以下Maven坐标下提供Bill of Materials POM来简化依赖关系管理 。
org.junit
junit-bom
5.3.2
也就是:
<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文件部分内容:
<!--此处注意应该放在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 具有直接或传递依赖性。所以当需要此依赖时,就需要加上了。
org.opentest4j
opentest4j
1.1.1
也就是:
<dependency>
<groupId>org.opentest4j</groupId>
<artifactId>opentest4j</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>