junit5 实践

2024-05-13 16:48
文章标签 实践 junit5

本文主要是介绍junit5 实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

网上有若干的junit5的教程, 可惜好多的跑不起来, 所以决定自己写一个, 作为junit4的升级版本, 还是有很多的长进的.

项目的junit5的依赖是:

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><junit.version>4.12</junit.version><junit.jupiter.version>5.5.2</junit.jupiter.version><junit.vintage.version>5.5.2</junit.vintage.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>junit</groupId><artifactId>junit</artifactId><version>${junit.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><dependency><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId><version>${junit.vintage.version}</version><scope>test</scope></dependency></dependencies>

 

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

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

 

测试方法:

package com.example.project;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;/*** @author zk* @Description:* @date 2019-10-25 14:16*/
@DisplayName("第一个测试类")   //展示的别名
public class MyFirstTest {/*** 每个单元测试前执行一次*/@AfterEachvoid afterEach(){System.out.println("afterEach");}@BeforeEachvoid beforeEach(){System.out.println("beforeEach");}/*** BeforeAll  必须是static 的  一个类的所有单元测试开始前只执行一次*/@BeforeAllstatic void beforeAll(){System.out.println("beforeAll");}/*** AfterAll  必须是static 的  一个类的所有单元测试后只执行一次*/@AfterAllstatic void afterAll(){System.out.println("afterall");}@DisplayName("第一个测试方法")@Testvoid test1(){System.out.println("test1");Assertions.assertEquals(2,1+1);}@DisplayName("第二个测试方法")@Testvoid test2(){System.out.println("test2");Assertions.assertNull(null);}}

 

第二个测试:

package com.example.project;import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;import java.time.Duration;
import java.util.concurrent.TimeUnit;import static org.junit.jupiter.api.Assertions.*;/*** @author zk* @Description:* @date 2019-10-25 14:25*/
@DisplayName("第二个测试类")
public class MySecondTest {@Testvoid test1(){assertTrue(2 == 2, () -> "Assertion messages can be lazily evaluated -- "+ "to avoid constructing complex messages unnecessarily.");}/*** 多个测试*/@Testvoid test2(){Assertions.assertAll("all",()->Assertions.assertEquals(2,1+1),()->Assertions.assertNotEquals(2,1+3));}/*** 超时的测试*/@Testvoid timeoutNotExceeded() {Assertions.assertTimeout(Duration.ofSeconds(3), () -> {// Perform task that takes less than 3sThread.sleep(3500);});}/*** 测试方法提供参数* @param argument*/@ParameterizedTest@ValueSource(ints = { 1, 2, 3 })void testWithValueSource(int argument) {assertTrue(argument > 0 && argument < 4);}@ParameterizedTest@EnumSource(TimeUnit.class)void testWithEnumSource(TimeUnit timeUnit) {assertNotNull(timeUnit);}/*** 这里测试报错... https://blog.csdn.net/baidu_27222643/article/details/75020104* @param fruit* @param rank*/@ParameterizedTest@CsvSource({"apple,         1","banana,        2","'lemon, lime', 0xF1"})void testWithCsvSource(String fruit, int rank) {assertNotNull(fruit);assertNotEquals(0, rank);}}

 

好了, junit 的测试就说到这里, 相比于junit4, 功能增加了好多.

这篇关于junit5 实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/986272

相关文章

MySQL 迁移至 Doris 最佳实践方案(最新整理)

《MySQL迁移至Doris最佳实践方案(最新整理)》本文将深入剖析三种经过实践验证的MySQL迁移至Doris的最佳方案,涵盖全量迁移、增量同步、混合迁移以及基于CDC(ChangeData... 目录一、China编程JDBC Catalog 联邦查询方案(适合跨库实时查询)1. 方案概述2. 环境要求3.

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实