JUnit5 快速指南
JUnit5 快速指南
version: junit5
1. 安装
在 pom 中添加依赖
<properties>
  <junit.jupiter.version>5.3.2</junit.jupiter.version>
</properties>
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
  </dependency>
</dependencies>
组件间依赖关系:

2. JUnit 注解
| Annotation | Description | 
|---|---|
@Test | 
Denotes that a method is a test method. Unlike JUnit 4’s @Test annotation, this annotation does not declare any attributes, since test extensions in JUnit Jupiter operate based on their own dedicated annotations. Such methods are inherited unless they are overridden. | 
@ParameterizedTest | 
Denotes that a method is a parameterized test. Such methods are inherited unless they are overridden. | 
@RepeatedTest | 
Denotes that a method is a test template for a repeated test. Such methods are inherited unless they are overridden. | 
@TestFactory | 
Denotes that a method is a test factory for dynamic tests. Such methods are inherited unless they are overridden. | 
@TestInstance | 
Used to configure the test instance lifecycle for the annotated test class. Such annotations are inherited. | 
@TestTemplate | 
Denotes that a method is a template for test cases designed to be invoked multiple times depending on the number of invocation contexts returned by the registered providers. Such methods are inherited unless they are overridden. | 
@DisplayName | 
Declares a custom display name for the test class or test method. Such annotations are not inherited. | 
@BeforeEach | 
Denotes that the annotated method should be executed before each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class; analogous to JUnit 4’s @Before. Such methods are inherited unless they are overridden. | 
@AfterEach | 
Denotes that the annotated method should be executed after each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class; analogous to JUnit 4’s @After. Such methods are inherited unless they are overridden. | 
@BeforeAll | 
Denotes that the annotated method should be executed before all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @BeforeClass. Such methods are inherited (unless they are hidden or overridden) and must be static (unless the "per-class" test instance lifecycle is used). | 
@AfterAll | 
Denotes that the annotated method should be executed after all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @AfterClass. Such methods are inherited (unless they are hidden or overridden) and must be static (unless the "per-class" test instance lifecycle is used). | 
@Nested | 
Denotes that the annotated class is a nested, non-static test class. @BeforeAll and @AfterAllmethods cannot be used directly in a @Nested test class unless the "per-class" test instance lifecycle is used. Such annotations are not inherited. | 
@Tag | 
Used to declare tags for filtering tests, either at the class or method level; analogous to test groups in TestNG or Categories in JUnit 4. Such annotations are inherited at the class level but not at the method level. | 
@Disabled | 
Used to disable a test class or test method; analogous to JUnit 4’s @Ignore. Such annotations are not inherited. | 
@ExtendWith | 
Used to register custom extensions. Such annotations are inherited. | 
3. 编写单元测试
3.1. 基本的单元测试类和方法
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class Junit5StandardTests {
    private static final Logger LOGGER = LoggerFactory.getLogger(Junit5StandardTests.class);
    @BeforeAll
    static void beforeAll() {
        LOGGER.info("call beforeAll()");
    }
    @BeforeEach
    void beforeEach() {
        LOGGER.info("call beforeEach()");
    }
    @Test
    void succeedingTest() {
        LOGGER.info("call succeedingTest()");
    }
    @Test
    void failingTest() {
        LOGGER.info("call failingTest()");
        // fail("a failing test");
    }
    @Test
    @Disabled("for demonstration purposes")
    void skippedTest() {
        LOGGER.info("call skippedTest()");
        // not executed
    }
    @AfterEach
    void afterEach() {
        LOGGER.info("call afterEach()");
    }
    @AfterAll
    static void afterAll() {
        LOGGER.info("call afterAll()");
    }
}
3.2. 定制测试类和方法的显示名称
支持普通字符、特殊符号、emoji
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("A special test case")
class JunitDisplayNameDemo {
    @Test
    @DisplayName("Custom test name containing spaces")
    void testWithDisplayNameContainingSpaces() { }
    @Test
    @DisplayName("╯°□°)╯")
    void testWithDisplayNameContainingSpecialCharacters() { }
    @Test
    @DisplayName("												
											JUnit5 快速指南的更多相关文章
	
								- [译] MongoDB Java异步驱动快速指南
		
导读 mongodb-java-driver是mongodb的Java驱动项目. 本文是对MongoDB-java-driver官方文档 MongoDB Async Driver Quick Tour ...
		 
						- 转:C++ Boost/tr1 Regex(正则表达式)快速指南
		
C++ Boost/tr1 Regex(正则表达式)快速指南 正则表达式自Boost 1.18推出,目前已经成为C++11(tr1)的标准部分. 本文以Boost 1.39正则表达式为基础,应该广泛适 ...
		 
						- (译)快速指南:用UIViewPropertyAnimator做动画
		
翻译自:QUICK GUIDE: ANIMATIONS WITH UIVIEWPROPERTYANIMATOR 译者:Haley_Wong iOS 10 带来了一大票有意思的新特性,像 UIViewP ...
		 
						- 【SFA官方翻译】使用 Kubernetes、Spring Boot 2.0 和 Docker 的微服务快速指南
		
[SFA官方翻译]使用 Kubernetes.Spring Boot 2.0 和 Docker 的微服务快速指南 原创: Darren Luo SpringForAll社区 今天 原文链接:https ...
		 
						- Emacs 快速指南(中文翻译)
		
  Emacs 快速指南 目录 1. 小结(SUMMARY) 2. 基本的光标控制(BASIC CURSOR CONTROL) 3. 如果 EMACS 失去响应(IF EMACS STOPS RESP ...
		 
						- 29 A Quick Guide to Go's Assembler 快速指南汇编程序:使用go语言的汇编器简介
		
A Quick Guide to Go's Assembler 快速指南汇编程序:使用go语言的汇编器简介 A Quick Guide to Go's Assembler Constants Symb ...
		 
						- Emacs 快速指南 - 原生中文手册
		
Emacs 快速指南 -折叠目录 1. 小结(SUMMARY) 2. 基本的光标控制(BASIC CURSOR CONTROL) 3. 如果 EMACS 失去响应(IF EMACS STOPS RES ...
		 
						- Docker网络基础:快速指南
		
Docker网络基础:快速指南 原文连接:http://blogxinxiucan.sh1.newtouch.com/2017/07/30/Docker网络基础:快速指南/ 了解有关扩展网络功能的默认 ...
		 
						- 从 C++ 到 Objective-C 的快速指南
		
简介 当我开始为iOS写代码的时候,我意识到,作为一个C++开发者,我必须花费更多的时间来弄清楚Objective-C中怪异的东西.这就是一个帮助C++专家的快速指南,能够使他们快速的掌握Apple的 ...
		 
		
	
随机推荐
	
									- odoo:开源 ERP/CRM 入门与实践
			
看了这张图,或许你对odoo有了一些兴趣. 这次就是和大家一起交流开源ERP/CRM系统:odoo 对以下读者有帮助:研发.产品.项目.市场.服务.运营.管理等. 一.背景趋势 社交网络.电商O2O: ...
			 
						- CentOS基本的命令与快捷建
			
由于我的计算机在安装linux系统时,计算机出现了问题,并没有安装ubuntu而是安装的centos.虽然两者属于linux的不同版本,但是在具体的操作上大同小异.在学习linux的各种指令和快捷键的 ...
			 
						- spring4笔记----spring4国际化
			
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www. ...
			 
						- 单用户实例添加DB账号
			
停止实例 net stop mssqlserver 以单用户启动实例,指定以sqlcmd连接 net start mssqlserver /m"SQLCMD" 以单用户启动实例,指 ...
			 
						- Alwayson查询主副本不同的JOB,Linkserver,Login
			
DECLARE @SQL AS VARCHAR(5000), @Primary AS VARCHAR(50), @Secondy AS VARCHAR(50); SELECT @Primary='AA ...
			 
						- WinServerDFS
			
DFS提供共享路径统一命名,且文件相互备份,具有高可用性. 1.在相应的服务器上安装服务. --命名空间,复制以及管理控制台的安装 install-windowsfeature fs-dfs-name ...
			 
						- SQL Server @@ERROR的小误区大Bug
			
在公司项目中看到有这样使用事务的: -- 开启事务 BEGIN TRAN ) ) BEGIN ROLLBACK TRAN END COMMIT TRAN 乍一看没啥问题,仔细思考就能发现有很大的问题. ...
			 
						- Windows Server 2016-管理站点复制(一)
			
可以使用Active Directory站点和服务管理单元来管理实现站点间复制拓扑的特定于站点的对象.这些对象存储在Active Directory域服务 (AD DS) 的站点容器中.同一个站点内的 ...
			 
						- 介绍一个比较了各种浏览器对于HTML5 等标准支持程度的网站
			
可以选择浏览器种类,版本,比较的功能 网站地址:https://caniuse.com/#comparison
			 
						- 测试TCP 和 UDP 端口的方法
			
测试 TCP 端口: telnel IP PORT nc -vz IP PORT 测试 UDP 端口: nc -vuz IP PORT 其中 -u 表示使用 udp 协议来进行测试. -u, --ud ...