版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/IO_Field/article/details/82585790
Junit5简述
Junit之前的版本是一个整体,而Junit5与其不同,它是由几个不同的模块组成,而这些模块分别来自三个不同的子项目中。在官方文档中,给出了这么一个JUnit5的等式:
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
- JUnit Platform
- 在JVM上 启动测试框架 的基础平台。
- 定义了
TestEngine API
,该API可用于开发在平台上运行的测试框架 - 一个从命令行或者 Gradle 和 Maven 插件来启动的控制台启动器,它就好比一个基于JUnit4的Runner在平台上运行任何TestEngine。
- JUnit Jupiter 是一个组合体,它是由在JUnit 5中编写测试和扩展的新 编程模型 和 扩展模型 组成。另外,Jupiter子项目还提供了一个TestEngine,用于在平台上运行基于Jupiter的测试。
- JUnit Vintage 提供了一个TestEngine,用于在平台上运行基于JUnit 3和JUnit 4的测试。
在不同的模块中,又生成了不同的子组件,以用于依赖。在添加依赖关系时,并不需要添加所有的子组件,只需要添加所需的子组件即可。
JUnit Platform
- Group ID: org.junit.platform
- Version: 1.2.0
- Artifact IDs:
junit-platform-commons
:JUnit内部通用类库/实用工具,它们仅用于JUnit框架本身,不支持任何外部使用,外部使用风险自负。junit-platform-console
:支持从控制台中发现和执行JUnit Platform上的测试。junit-platform-console-standalone
一个包含了Maven仓库中的 junit-platform-console-standalone 目录下所有依赖项的可执行JAR包。junit-platform-engine
:测试引擎的公共API。junit-platform-gradle-plugin
:支持使用 Gralde 来发现和执行JUnit Platform上的测试。junit-platform-launcher
:配置和加载测试计划的公共API – 典型的使用场景是IDE和构建工具。junit-platform-runner
:在一个JUnit 4环境中的JUnit Platform上执行测试和测试套件的运行器。junit-platform-suite-api
:在JUnit Platform上配置测试套件的注解。被 JUnit Platform运行器 所支持,也有可能被第三方的TestEngine实现所
支持。junit-platform-surefire-provider
:支持使用 Maven Surefire 来发现和执行JUnit Platform上的测试。
JUnit Jupiter
- Group ID: org.junit.vintage
- Version: 5.2.0
- Artifact ID:
junit-jupiter-api
:编写测试 和 扩展 的JUnit Jupiter API。junit-jupiter-engine
:JUnit Jupiter测试引擎的实现,仅仅在运行时需要。junit-jupiter-params
:支持JUnit Jupiter中的 参数化测试。junit-jupiter-migration-support
:支持从JUnit 4迁移到JUnit Jupiter,仅在使用了JUnit 4规则的测试中才需要
JUnit Vintage
- Group ID: org.junit.vintage
- Version: 5.2.0
- Artifact ID:
junit-vintage-engine
:JUnit Vintage测试引擎实现,允许在新的JUnit Platform上运行低版本的JUnit测试,即那些以JUnit 3或JUnit 4风格编写的测试。
Gradle依赖示例
// JUnit
// org.junit.platform
testImplementation 'org.junit.platform:junit-platform-launcher:1.2.0'
testImplementation 'org.junit.platform:junit-platform-console:1.2.0'
testImplementation 'org.junit.platform:junit-platform-console-standalone:1.2.0'
testImplementation 'org.junit.platform:junit-platform-engine:1.2.0'
testImplementation 'org.junit.platform:junit-platform-gradle-plugin:1.2.0'
testImplementation 'org.junit.platform:junit-platform-runner:1.2.0'
testImplementation 'org.junit.platform:junit-platform-suite-api:1.2.0'
testImplementation 'org.junit.platform:junit-platform-surefire-provider:1.2.0'
// org.junit.jupiter
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.2.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-params:5.2.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-migration-support:5.2.0'
// org.junit.vintage
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.2.0'
依赖图例
测试用例
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class FirstTests {
@Test
void myFirstTest() {
assertEquals(2, 1 + 1);
}
}
在上面的测试用例中:
- 使用
myFirstTest
方法上方使用@Test
方法,表明该方法是一个测试方法。 - 在
myFirstTest
方法中,使用断言assertEquals
,用以验证2
和1+1
的值是否相等