当前位置:网站首页>测试类中的断言机制
测试类中的断言机制
2022-07-27 21:10:00 【把星星枕在床边】
断言(assertions):断言(assertions)是测试方法中的核心部分,用来对测试需要满足的条件进行验证。这些断言方法都是 org.junit.jupiter.api.Assertions 的静态方法。JUnit 5 内置的断言可以分成如下几个类别:
检查业务逻辑返回的数据是否合理。
所有的测试运行结束以后,会有一个详细的测试报告;
1、简单断言
用来对单个值进行简单的验证。如:
方法 说明
assertEquals 判断两个对象或两个原始类型是否相等
assertNotEquals 判断两个对象或两个原始类型是否不相等
assertSame 判断两个对象引用是否指向同一个对象
assertNotSame 判断两个对象引用是否指向不同的对象
assertTrue 判断给定的布尔值是否为 true
assertFalse 判断给定的布尔值是否为 false
assertNull 判断给定的对象引用是否为 null
assertNotNull 判断给定的对象引用是否不为 null
代码实现:
@Test
@DisplayName("simple assertion")
public void simple() {
assertEquals(3, 1 + 2, "simple math");
assertNotEquals(3, 1 + 1);
assertNotSame(new Object(), new Object());
Object obj = new Object();
assertSame(obj, obj);
assertFalse(1 > 2);
assertTrue(1 < 2);
assertNull(null);
assertNotNull(new Object());
}
2、数组断言
通过 assertArrayEquals 方法来判断两个对象或原始类型的数组是否相等
@Test
@DisplayName("array assertion")
public void array() {
assertArrayEquals(new int[]{
1, 2}, new int[] {
1, 2});
}
3、组合断言
assertAll 方法接受多个 org.junit.jupiter.api.Executable 函数式接口的实例作为要验证的断言,可以通过 lambda 表达式很容易的提供这些断言
@Test
@DisplayName("assert all")
public void all() {
assertAll("Math",
() -> assertEquals(2, 1 + 1),
() -> assertTrue(1 > 0)
);
}
4、异常断言
在JUnit4时期,想要测试方法的异常情况时,需要用@Rule注解的ExpectedException变量还是比较麻烦的。而JUnit5提供了一种新的断言方式Assertions.assertThrows() ,配合函数式编程就可以进行使用。
@Test
@DisplayName("异常测试")
public void exceptionTest() {
ArithmeticException exception = Assertions.assertThrows(
//扔出断言异常
ArithmeticException.class, () -> System.out.println(1 % 0));
}
5、超时断言
Junit5还提供了Assertions.assertTimeout() 为测试方法设置了超时时间
@Test
@DisplayName("超时测试")
public void timeoutTest() {
//如果测试方法时间超过1s将会异常
Assertions.assertTimeout(Duration.ofMillis(1000), () -> Thread.sleep(500));
}
6、快速失败
通过 fail 方法直接使得测试失败
@Test
@DisplayName("fail")
public void shouldFail() {
fail("This should fail");
}
断言在我们的开发中是一项十分重要的功能,在开发中通常我们写完业务逻辑模块之后在上线之前,我们就得先写一个单元测试类进行单元测试。它就会给我们一个完整的汇总报告,哪些成了哪些失败,就会给我们一个精准的定位。
使用Maven的中的test功能,只有测试成功之后我们才打包
边栏推荐
猜你喜欢

Remotely debug idea, configure remote debug, and add JVM startup parameter -xdebug in the program of remote server

【zer0pts CTF 2022】 Anti-Fermat

(十二)51单片机----用DS18B20浅测一下工(江)西的室外温度

Design and implementation of spark offline development framework

Realize today's news website based on native JS

C # delegate usage -- console project, which implements events through delegation

QT with OpenGL (shadow mapping)

Unity 实现简单画板画画功能(笔记)

How to use C WinForm to copy files and display progress

Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
随机推荐
File & recursion 14.1
为什么 Redis 集群要使用反向代理? 看这篇就明白了
QT with OpenGL (shadow mapping)
BUU-CTF basic rsa
NDK series (6): let's talk about the way and time to register JNI functions
[NPUCTF2020]EzRSA
How to use FTP to realize automatic update of WinForm
总投资600亿!富士康半导体高端封测项目正式落户青岛
Interviewer: let's talk about the specific process of network data transmission
Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?
Elk log analysis system installation and deployment
Arm32进行远程调试
Monologue of a software Investor: why don't I pursue fast-growing companies
TSMC 3nm detail exposure: transistor density as high as 250million /mm ², Greatly improved performance and energy efficiency
Redis 哈希Hash底层数据结构
smartRefresh嵌套多个RecycleView滑动冲突及布局显示不全
[GWCTF 2019]BabyRSA1
Put cloudflare on the website (take Tencent cloud as an example)
Error:svn: E155010: ‘/Users/.../Desktop/wrokspace/xxx‘ is scheduled for addition, but is missing
Shuffle, partition and read of tfrecord