单元测试之JUnit5入门

2024-03-01 22:32
文章标签 入门 单元测试 junit5

本文主要是介绍单元测试之JUnit5入门,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

关于JUnit5

与以前版本的JUnit不同,JUnit 5由三个不同子项目中的几个不同模块组成。

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

  • JUnit Platform是基于JVM的运行测试的基础框架在,它定义了开发运行在这个测试框架上的TestEngine API。此外该平台提供了一个控制台启动器,可以从命令行启动平台,可以为Gradle和 Maven构建插件,同时提供基于JUnit 4的Runner。
  • JUnit Jupiter是在JUnit 5中编写测试和扩展的新编程模型和扩展模型的组合.Jupiter子项目提供了一个TestEngine在平台上运行基于Jupiter的测试。
  • JUnit Vintage提供了一个TestEngine在平台上运行基于JUnit 3和JUnit 4的测试。

引入依赖包

<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.0.3</version><scope>test</scope>
</dependency>
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope>
</dependency>

编写单元测试

JUnit Hello world

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;class FirstJUnit5Tests {@Testvoid myFirstTest() {assertEquals(2, 1 + 1);}
}

@Test注解在方法上标记方法为测试方法,以便构建工具和 IDE 能够识别并执行它们。JUnit 5不再需要手动将测试类与测试方法为public,包可见的访问级别就足够了。

初始化与销毁

你可能需要执行一些代码来在测试执行前后完成一些初始化或销毁的操作。在 JUnit 5 中,有4个注解你可能会用于如此工作:

  • @BeforeAll 只执行一次,执行时机是在所有测试和 @BeforeEach 注解方法之前。
  • @BeforeEach 在每个测试执行之前执行。
  • @AfterEach 在每个测试执行之后执行。
  • @AfterAll只执行一次,执行时机是在所有测试和 @AfterEach 注解方法之后。

因为框架会为每个测试创建一个单独的实例,在 @BeforeAll/@AfterAll 方法执行时尚无任何测试实例诞生。因此,这两个方法必须定义为静态方法。


import org.junit.jupiter.api.*;
class StandardTests {@BeforeAllstatic void initAll() {}@BeforeEachvoid init() {}@Testvoid succeedingTest() {}@Testvoid failingTest() {fail("a failing test");}@Test@Disabled("for demonstration purposes")void skippedTest() {// not executed}@AfterEachvoid tearDown() {}@AfterAllstatic void tearDownAll() {}}

断言

准备好测试实例、执行了被测类的方法以后,断言能确保你得到了想要的结果。一般的断言,无非是检查一个实例的属性(比如,判空与判非空等),或者对两个实例进行比较(比如,检查两个实例对象是否相等)等。无论哪种检查,断言方法都可以接受一个字符串作为最后一个可选参数,它会在断言失败时提供必要的描述信息。如果提供出错信息的过程比较复杂,它也可以被包装在一个 lambda 表达式中,这样,只有到真正失败的时候,消息才会真正被构造出来。

class AssertionsDemo {@Testvoid standardAssertions() {assertEquals(2, 2);assertEquals(4, 4, "The optional assertion message is now the last parameter.");assertTrue(2 == 2, () -> "Assertion messages can be lazily evaluated -- "+ "to avoid constructing complex messages unnecessarily.");}@Testvoid groupedAssertions() {// In a grouped assertion all assertions are executed, and any// failures will be reported together.assertAll("person",() -> assertEquals("John", person.getFirstName()),() -> assertEquals("Doe", person.getLastName()));}@Testvoid dependentAssertions() {// Within a code block, if an assertion fails the// subsequent code in the same block will be skipped.assertAll("properties",() -> {String firstName = person.getFirstName();assertNotNull(firstName);// Executed only if the previous assertion is valid.assertAll("first name",() -> assertTrue(firstName.startsWith("J")),() -> assertTrue(firstName.endsWith("n")));},() -> {// Grouped assertion, so processed independently// of results of first name assertions.String lastName = person.getLastName();assertNotNull(lastName);// Executed only if the previous assertion is valid.assertAll("last name",() -> assertTrue(lastName.startsWith("D")),() -> assertTrue(lastName.endsWith("e")));});}@Testvoid exceptionTesting() {Throwable exception = assertThrows(IllegalArgumentException.class, () -> {throw new IllegalArgumentException("a message");});assertEquals("a message", exception.getMessage());}@Testvoid timeoutNotExceeded() {// The following assertion succeeds.assertTimeout(ofMinutes(2), () -> {// Perform task that takes less than 2 minutes.});}@Testvoid timeoutNotExceededWithResult() {// The following assertion succeeds, and returns the supplied object.String actualResult = assertTimeout(ofMinutes(2), () -> {return "a result";});assertEquals("a result", actualResult);}@Testvoid timeoutNotExceededWithMethod() {// The following assertion invokes a method reference and returns an object.String actualGreeting = assertTimeout(ofMinutes(2), AssertionsDemo::greeting);assertEquals("hello world!", actualGreeting);}@Testvoid timeoutExceeded() {// The following assertion fails with an error message similar to:// execution exceeded timeout of 10 ms by 91 msassertTimeout(ofMillis(10), () -> {// Simulate task that takes more than 10 ms.Thread.sleep(100);});}@Testvoid timeoutExceededWithPreemptiveTermination() {// The following assertion fails with an error message similar to:// execution timed out after 10 msassertTimeoutPreemptively(ofMillis(10), () -> {// Simulate task that takes more than 10 ms.Thread.sleep(100);});}private static String greeting() {return "hello world!";}}

迁移指南
JUnit 平台可以通过 Jupiter 引擎来运行 JUnit 5 测试,Vintage 引擎来运行 JUnit 3 和 JUnit 4 测试。因此,已有的 JUnit 3 和 4 的测试不需要任何修改就可以直接在 JUnit 平台上运行。只需要确保 Vintage 引擎的 jar 包出现在 classpath 中,JUnit 平台会自动发现并使用该引擎来运行 JUnit 3 和 4 测试。开发人员可以按照自己的项目安排来规划迁移到 JUnit 5 的进度。可以保持已有的 JUnit 3 和 4 的测试用例不变,而新增加的测试用例则使用 JUnit 5。

在进行迁移的时候需要注意如下的变化:

  • 注解在 org.junit.jupiter.api 包中,断言在 org.junit.jupiter.api.Assertions 类中,前置条件在 org.junit.jupiter.api.Assumptions 类中。
  • 把@Before 和@After 替换成@BeforeEach 和@AfterEach。
  • 把@BeforeClass 和@AfterClass 替换成@BeforeAll 和@AfterAll。
  • 把@Ignore 替换成@Disabled。
  • 把@Category 替换成@Tag。
  • 把@RunWith、@Rule 和@ClassRule 替换成@ExtendWith。

本文主要内容翻译和整合了以下网络文章

  1. JUnit 5 用户指南
  2. JUnit 5 系列:基础入门
  3. JUnit 5 新特性

这篇关于单元测试之JUnit5入门的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/764036

相关文章

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三

从入门到精通详解LangChain加载HTML内容的全攻略

《从入门到精通详解LangChain加载HTML内容的全攻略》这篇文章主要为大家详细介绍了如何用LangChain优雅地处理HTML内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录引言:当大语言模型遇见html一、HTML加载器为什么需要专门的HTML加载器核心加载器对比表二

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE