SpringBoot 2.6 和 JUnit 5 的测试用例注解和排序方式
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 的测试用例注解和排序方式的更多相关文章
- SpringBoot项目下的JUnit测试
		
在SpringBoot项目里,要编写单元测试用例,需要依赖4个jar.一个是最基本的JUnit,然后是spring-test和spring-boot-test. <!--test--> & ...
 - SpringBoot介绍,快速入门小例子,目录结构,不同的启动方式,SpringBoot常用注解
		
SpringBoot介绍 引言 为了使用ssm框架去开发,准备ssm框架的模板配置 为了Spring整合第三方框架,单独的去编写xml文件 导致ssm项目后期xml文件特别多,维护xml文件的成本也是 ...
 - Junit中常用的注解说明
		
Java注解((Annotation)的使用方法是@注解名 ,能通过简单的词语来实现一些功能.在junit中常用的注解有@Test.@Ignore.@BeforeClass.@AfterClass.@ ...
 - JUnit常用断言及注解
		
断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过. 断言核心方法 assertArrayEquals(expecteds, actuals) 查看两个数组 ...
 - Junit框架使用--JUnit常用断言及注解
		
从别人博客中抄过来一点东西 原文地址:http://blog.csdn.net/wangpeng047/article/details/9628449 断言是编写测试用例的核心实现方式,即期望值是多少 ...
 - 【Springboot】Springboot整合Jasypt,让配置信息安全最优雅方便的方式
		
1 简介 在上一篇文章中,介绍了Jasypt及其用法,具体细节可以查看[Java库]如何使用优秀的加密库Jasypt来保护你的敏感信息?.如此利器,用之得当,那将事半功倍.本文将介绍Springboo ...
 - Junit框架使用(4)--JUnit常用断言及注解
		
从别人博客中抄过来一点东西 原文地址:http://blog.csdn.net/wangpeng047/article/details/9628449 断言是编写测试用例的核心实现方式,即期望值是多少 ...
 - SpringBoot整合SpringSecurity示例实现前后分离权限注解
		
SpringBoot 整合SpringSecurity示例实现前后分离权限注解+JWT登录认证 作者:Sans_ juejin.im/post/5da82f066fb9a04e2a73daec 一.说 ...
 - SpringBoot框架下基于Junit的单元测试
		
前言 Junit是一个Java语言的单元测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量.是一个在发展,现在已经到junit5,在javaEE开发中与很多框架相集成 ...
 - springboot  搭建 简单 web项目 【springboot + freemark模板 + yml 配置文件 + 热修复 + 测试用例】附源码
		
项目 地址: https://gitee.com/sanmubird/springboot-simpleweb.git 项目介绍: 本项目主要有一下内容: 1: springboot yml 配置 ...
 
随机推荐
- 15-Verilog Coding Style
			
Verilog Coding Style 1.为什么需要Coding Style 可综合性 - 代码需要综合成网表,如果写了一些不可综合的代码,会出现错误 可读性,代码通常有多个版本,所以需要保证代码 ...
 - 缓存选型:Redis or MemCache
			
★ Redis24篇集合 1 背景 互联网产品为了保证高性能和高可用性,经常会使用缓存来进行架构设计.最常用的就是使用Redis了,也有部分企业会选择使用Memcache. 所以了解 Redis 和 ...
 - [转帖]彻底搞明白 GB2312、GBK 和 GB18030
			
https://zhuanlan.zhihu.com/p/453675608 日常工作的过程中,关于字符编码的问题经常让人头疼不已,这篇文章就来捋一捋关于 GB2312.GBK.GB18030 相关的 ...
 - [转帖]Linux内存之Cache
			
一. Linux内存之Cache 1.1.Cache 1.1.1.什么是Cache? Cache存储器,是位于CPU和主存储器DRAM之间的一块高速缓冲存储器,规模较小,但是速度很快,通常由SRAM( ...
 - [转帖]事务上的等待事件 —— enq: TM - contention
			
执行DML期间,为防止对与DML相关的对象进行修改,执行DML的进程必须对该表获得TM锁.若在获得TM锁的过程中发生争用,则等待enq: HW - contention 事件. SQL> sel ...
 - TiKV占用内存超过的解决过程
			
TiKV占用内存超过的解决过程 背景 为了后去TiDB的极限数据. 晚上在每台服务器上面增加了多个TiKV的节点. 主要方式为: 每个NVME的硬盘增加两个TiKV的进程. 这样每个服务器两个磁盘, ...
 - [转帖]CertUtil: -hashfile 失败: 0xd00000bb (-805306181)
			
https://www.cnblogs.com/heenhui2016/p/de.html 使用CertUtil验证Python安装文件的时候出现了这个错误. CertUtil: -hashfile ...
 - 【技术剖析】7. 看看毕昇 JDK 团队是如何解决 JVM 中 CMS 的 Crash
			
[技术剖析]7. 看看毕昇 JDK 团队是如何解决 JVM 中 CMS 的 Crashhttps://bbs.huaweicloud.com/forum/thread-168485-1-1.html ...
 - 计划任务方式定期获取jvm dump的方法
			
说明 产品最近有一些问题,想着能够每隔一段时间抓取一下dump文件. 需求 可以定期抓取, 需要注意磁盘空间的使用. 实现方法 定时任务使用 crontab 计划任务来做 预定义获取jvm dump的 ...
 - Raid卡在Write back 与Write through 时的性能差异
			
还是读姜老师的 mysql技术内核innodb存储引擎这本书里面的内容. 之前知道raid卡的设置会影响性能, 预计也是十几倍的性能差距, 但是从来没有用数据库进行过验证 书中有针对不通raid卡的设 ...