junit5 入门系列教程-01-junit5 简单入门例子
Junit5 系列索引
目录
Junit5
is the next generation of JUnit.
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 导入
<dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.2.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.2.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>5.2.0</version> <scope>test</scope> </dependency> </dependencies>
测试案例
-
FirstJUnit5Tests.java
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class FirstJUnit5Tests { @Test void myFirstTest() { assertEquals(2, 1+1); } }
变化
-
导入的包路径和原来不同 方法不必再声明为 public