JUnit5使用总结

简介: JUnit5使用总结

要使用的话,在build.gradle 里要添加

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.+';
    }
    test {
      useJUnitPlatform();
  }

看一下几个注解在junit4和junit5版本不同的写法:

junit4 junit5    使用场景
@BeforeClass @BeforeAll  在当前类的所有测试方法之前执行。注解在【静态方法】上
@AfterClass @AfterAll  在当前类中的所有测试方法之后执行。注解在【静态方法】上
@Before @BeforeEach 在每个测试方法之前执行。注解在【非静态方法】上
@After @AfterEach 在每个测试方法之后执行。注解在【非静态方法】上

   

  使用方式如下:

import org.junit.*;
public class TestJunitAnnotation {
    @BeforeClass
    public static void beforeClass(){
        System.out.println("before class:begin this class*************");
    }
    @AfterClass
    public static void afterClass(){
        System.out.println("after class:end this class--------------");
    }
    @Before
    public void before(){
        System.out.println("before:begin test");
    }
    @After
    public void after(){
        System.out.println("after:end test");
    }
    @Test
    public void Test(){
        System.out.println("[this is a test!]");
    }
    @Test
    public void Test2(){
        System.out.println("[this is another test!]");
    }
}

@test测试先后顺序

以下是junit4的使用,在类声明上加

 

1.从上到下 执行@Test,这个不靠谱
@FixMethodOrder(MethodSorters.JVM)
2.按方法名字顺序执行
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
3.默认方法,不可预期
@FixMethodOrder(MethodSorters.DEFAULT)

以下是junit5的使用方式

//按照数字顺序
@TestMethodOrder (MethodOrderer.OrderAnnotation.class)
public class OOXXTest {
@Test
@Order(1)
public void one() throws Exception {
}
@Test
@Order(2)
public void two() throws Exception {
}
//字母数字顺序
@TestMethodOrder (MethodOrderer.Alphanumeric.class) 
//随机排序
@TestMethodOrder (MethodOrderer.Random.class)

@DisplayName可以让测试结果显示更加nice

可以对类和方法做注释

@DisplayName("压缩测试")
@TestMethodOrder (MethodOrderer.OrderAnnotation.class)
public class CompressTest {...}
@Test
@DisplayName("Zstandard解压缩")
@Order(2)
public void zstandarDecompress() throws Exception
{...}

对某个方法进行重复测试

@RepeatedTest(value = 3, name = "{displayName} 第 {currentRepetition}/{totalRepetitions} 次")
public void zlibDecompress() throws Exception {}

参数化测试,可以用于很多不同参数的数据测试

@ParameterizedTest
@ValueSource(strings = {"one", "two", "three"})
@DisplayName("参数化测试")
public void parameterizedTest(String string) {
  System.out.println(string);
  Assertions.assertTrue(StringUtils.isNotBlank(string));
}

最后看下效果:

对同一个方法进行多次测试

@RepeatedTest(10000)

如何进行多线程测试,首先要在resources目录下创建文件junit-platform.properties,内容如下:

junit.jupiter.execution.parallel.enabled=true
#类内部方法并行
junit.jupiter.execution.parallel.mode.default = concurrent
#类之间串行
junit.jupiter.execution.parallel.mode.classes.default = same_thread
# the maximum pool size can be configured using a ParallelExecutionConfigurationStrategy
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=8

然后,在想要多线程执行的类的方法上加上注解:

@Execution(ExecutionMode.CONCURRENT)

如果临时性的不想跑部分测试方法,只需要在方法上加

@Disabled

参考:

该升级你的JUnit版本了——JUnit5基本介绍

目录
相关文章
|
人工智能 JSON 算法
向量嵌入的天花板与AI检索的模式更迭
本文提出突破传统“单向量嵌入+ANN”检索范式,构建多结构协同的下一代AI检索框架。通过多通道嵌入、组合键兜底、知识图推理、程序化计划与生成-校验闭环,实现高可信、可解释、可验证的智能检索,应对复杂任务中的信息漏检与推理难题,推动RAG迈向结构化、可编程的认知系统。
563 12
|
数据采集 人工智能 监控
40.8K star!让AI帮你读懂整个互联网:Crawl4AI开源爬虫工具深度解析
Crawl4AI 是2025年GitHub上备受瞩目的开源网络爬虫工具,专为AI时代设计。它不仅能抓取网页内容,还能理解页面语义结构,生成适配大语言模型的训练数据格式。上线半年获4万+星标,应用于1200+AI项目。其功能亮点包括智能内容提取引擎、AI就绪数据管道和企业级特性,支持动态页面处理、多语言识别及分布式部署。技术架构基于Python 3.10与Scrapy框架,性能卓越,适用于AI训练数据采集、行业情报监控等场景。相比Scrapy、BeautifulSoup等传统工具,Crawl4AI在动态页面支持、PDF解析和语义分块方面更具优势
5638 0
40.8K star!让AI帮你读懂整个互联网:Crawl4AI开源爬虫工具深度解析
|
C语言 索引 Python
Python零基础入门-3 流程控制
Python零基础入门-3 流程控制
239 0
|
JavaScript
解决报错问题:‘ts-node‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
解决报错问题:‘ts-node‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
1915 0
|
缓存 JSON NoSQL
缓存实战(一)剖析缓存常见问题
缓存实战(一)剖析缓存常见问题
350 1
缓存实战(一)剖析缓存常见问题
|
安全 网络协议 网络安全
2021年中职“网络安全“江西省赛题—B-6:流量分析
2021年中职“网络安全“江西省赛题—B-6:流量分析
693 0
|
SQL 监控 关系型数据库
一次数据库CPU飙高问题排查与解决
### 问题发现 最近,经常收到一些数据库的报警,提示我们的数据库的CPU有异常飙高的情况,通过该监控发现,确实间歇性的有一些CPU飙高的情况,经常把CPU打满了。 ![](https://ata2-img.oss-cn-zhangjiakou.aliyuncs.com/neweditor/a1786489-1f44-4c39-bea4-85ca25a45433.png) ### 问题排
1286 0
一次数据库CPU飙高问题排查与解决
|
JavaScript
eslint全局变量报错 xxx is not defined
eslint全局变量报错 xxx is not defined
1055 0
|
Linux
linux设置时钟同步
redhat 时钟同步 ntp
487 0
linux设置时钟同步
|
前端开发
前端学习笔记202307学习笔记第六十一天-拦截器工作原理2
前端学习笔记202307学习笔记第六十一天-拦截器工作原理2
190 0