JUnit5 的测试注解

在JUnit5中, 不再使用 @RunWith 注解, 改为使用 @ExtendWith(SpringExtension.class)

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class AccountTest {
@Resource
AccountMapper accountMapper; @Test
void test1_list() {
System.out.println("hello");
List<AccountDTO> accounts = accountMapper.list(new HashMap<>());
System.out.println(accounts.size());
}
}

对比JUnit4的测试注解

在JUnit4中, 使用的测试注解(非SpringBoot)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring/spring-commons.xml"})
public class IpBlacklistTest {
private static final Logger logger = LoggerFactory.getLogger(IpBlacklistTest.class);
@Resource(name="sessionTokenDTOService")
private SessionTokenDTOService sessionTokenDTOService; @Test
public void test() {
long from = TimeUtil.shiftByHour(new Date(), 48, 0).getTime();
ResultDTO result = sessionTokenDTOService.resetIpBlackList(from, 100);
System.out.println(result.getMessage());
}
}

在SrpingBoot中为

@RunWith(SpringRunner.class)
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RabbitMQTest {
private static Logger logger = LoggerFactory.getLogger(RabbitMQTest.class);
private static final TimeflakeId SFID = new TimeflakeId(100); @Resource(name="requestPOService")
private RequestPOService requestPOService; @Autowired
private SysConfig sysConfig; @Test
public void test1_freeze() {
EsbEntity<Head, Freeze> request = requestPOService.genPiaFreezRequest(302040, 100);
requestPOService.send(JacksonUtils.compressObject(request));
}
}

测试用例的顺序

https://www.baeldung.com/junit-5-test-order

In JUnit 5, we can use @TestMethodOrder to control the execution order of tests.

排序方式有

  • Alphanumeric
  • OrderAnnotation
  • Random
  • Custom Order

按方法字符排序

@TestMethodOrder(MethodOrderer.MethodName.class)
public class AlphanumericOrderUnitTest {
private static StringBuilder output = new StringBuilder(""); @Test
public void myATest() {
output.append("A");
} @Test
public void myBTest() {
output.append("B");
} @Test
public void myaTest() {
output.append("a");
} @AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ABa");
}
}

使用 @Order 注解排序

@TestMethodOrder(OrderAnnotation.class)
public class OrderAnnotationUnitTest {
private static StringBuilder output = new StringBuilder(""); @Test
@Order(1)
public void firstTest() {
output.append("a");
} @Test
@Order(2)
public void secondTest() {
output.append("b");
} @Test
@Order(3)
public void thirdTest() {
output.append("c");
} @AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "abc");
}
}

使用随机排序

使用 MethodOrderer.Random 实现的伪随机排序测试

@TestMethodOrder(MethodOrderer.Random.class)
public class RandomOrderUnitTest { private static StringBuilder output = new StringBuilder(""); @Test
public void myATest() {
output.append("A");
} @Test
public void myBTest() {
output.append("B");
} @Test
public void myCTest() {
output.append("C");
} @AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ACB");
}
}

使用自定义排序

public class CustomOrder implements MethodOrderer {
@Override
public void orderMethods(MethodOrdererContext context) {
context.getMethodDescriptors().sort(
(MethodDescriptor m1, MethodDescriptor m2)->
m1.getMethod().getName().compareToIgnoreCase(m2.getMethod().getName()));
}
} @TestMethodOrder(CustomOrder.class)
public class CustomOrderUnitTest { // ... @AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "AaB");
}
}

SpringBoot 2.6 和 JUnit 5 的测试用例注解和排序方式的更多相关文章

  1. SpringBoot项目下的JUnit测试

    在SpringBoot项目里,要编写单元测试用例,需要依赖4个jar.一个是最基本的JUnit,然后是spring-test和spring-boot-test. <!--test--> & ...

  2. SpringBoot介绍,快速入门小例子,目录结构,不同的启动方式,SpringBoot常用注解

    SpringBoot介绍 引言 为了使用ssm框架去开发,准备ssm框架的模板配置 为了Spring整合第三方框架,单独的去编写xml文件 导致ssm项目后期xml文件特别多,维护xml文件的成本也是 ...

  3. Junit中常用的注解说明

    Java注解((Annotation)的使用方法是@注解名 ,能通过简单的词语来实现一些功能.在junit中常用的注解有@Test.@Ignore.@BeforeClass.@AfterClass.@ ...

  4. JUnit常用断言及注解

    断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过. 断言核心方法   assertArrayEquals(expecteds, actuals) 查看两个数组 ...

  5. Junit框架使用--JUnit常用断言及注解

    从别人博客中抄过来一点东西 原文地址:http://blog.csdn.net/wangpeng047/article/details/9628449 断言是编写测试用例的核心实现方式,即期望值是多少 ...

  6. 【Springboot】Springboot整合Jasypt,让配置信息安全最优雅方便的方式

    1 简介 在上一篇文章中,介绍了Jasypt及其用法,具体细节可以查看[Java库]如何使用优秀的加密库Jasypt来保护你的敏感信息?.如此利器,用之得当,那将事半功倍.本文将介绍Springboo ...

  7. Junit框架使用(4)--JUnit常用断言及注解

    从别人博客中抄过来一点东西 原文地址:http://blog.csdn.net/wangpeng047/article/details/9628449 断言是编写测试用例的核心实现方式,即期望值是多少 ...

  8. SpringBoot整合SpringSecurity示例实现前后分离权限注解

    SpringBoot 整合SpringSecurity示例实现前后分离权限注解+JWT登录认证 作者:Sans_ juejin.im/post/5da82f066fb9a04e2a73daec 一.说 ...

  9. SpringBoot框架下基于Junit的单元测试

    前言 Junit是一个Java语言的单元测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量.是一个在发展,现在已经到junit5,在javaEE开发中与很多框架相集成 ...

  10. springboot 搭建 简单 web项目 【springboot + freemark模板 + yml 配置文件 + 热修复 + 测试用例】附源码

    项目 地址:  https://gitee.com/sanmubird/springboot-simpleweb.git 项目介绍: 本项目主要有一下内容: 1: springboot yml 配置 ...

随机推荐

  1. 03-ASIC和FPGA中的时钟结构

    1 ASIC 中时钟的结构 ASIC电路中的时钟的结构.这是一个非常典型的MCU的时钟结构图.它的时钟结构和功能的划分.首先,我们通过外部振荡器发送了一个8MHz的时钟给PLL,经过分分频和倍频产生更 ...

  2. 精通 VS 调试技巧,学习与工作效率翻倍!

    ​ 欢迎大家来到贝蒂大讲堂 ​ 养成好习惯,先赞后看哦~ ​ 所属专栏:C语言学习 ​ 贝蒂的主页:Betty's blog ​ 1. 什么是调试 当我们写代码时候常常会遇见输出结果不符合我们预期的情 ...

  3. Oracle session的sid与serial的简单学习

    Oracle session的sid与serial的简单学习 ITPUB vage的说法 这样说吧,Oracle允许的会话数(或者说连接数)是固定的,比如是3000个.假设每个会话要占1K字节,哪一共 ...

  4. [转帖]ORACLE恢复神器之ODU/AUL/DUL

    https://www.cnblogs.com/oracle-dba/p/3873870.html 分享ORACLE数据库恢复神器之ODU.DUL和AUL工具. ODU:ORACLE DATABASE ...

  5. java 调优需要关闭的组建

  6. vivo 海量基础数据计算架构应用实践

    作者:来自 vivo 互联网大数据团队 本文根据刘开周老师在"2023 vivo开发者大会"现场演讲内容整理而成.公众号回复[2023 VDC]获取互联网技术分会场议题相关资料. ...

  7. React中函数组件与类组件的两种使用

    React 创建组件的两种方式 函数组件:使用js函数创建的组件 约定1:函数名称必须以大写字母开头 约定2:函数组件必须要有返回值. 如果返回值为null.表示不渲染任何内容. return nul ...

  8. Leetcode 2题 两数相加

    题目链接 https://leetcode-cn.com/problems/add-two-numbers/ 题目描述 给你两个非空的链表,表示两个非负的整数.它们每位数字都是按照逆序的方式存储的,并 ...

  9. 强化学习技巧三:Python多进程

    1.Python多进程模块 Python中的多进程是通过multiprocessing包来实现的,和多线程的threading.Thread差不多,它可以利用multiprocessing.Proce ...

  10. 找工作、备考、面试刷题网站推荐(牛客网、力扣、计蒜客、hihocoder、七月在线)以及acm竞赛oj

    不管是找工作笔试面试白板试进大厂,还是研究生参加初试复试,数据结构和算法都是都是重中之重,刷题就很必要,来拿走自己的offer 吧! 一.offer刷题推荐 1.牛客网 链接:牛客网 - 找工作神器| ...