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

相关文章

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

linux安装、更新、卸载anaconda实践

《linux安装、更新、卸载anaconda实践》Anaconda是基于conda的科学计算环境,集成1400+包及依赖,安装需下载脚本、接受协议、设置路径、配置环境变量,更新与卸载通过conda命令... 目录随意找一个目录下载安装脚本检查许可证协议,ENTER就可以安装完毕之后激活anaconda安装更