当前位置:网站首页>Assertion mechanism in test class
Assertion mechanism in test class
2022-07-27 23:56:00 【Put the stars by the bed】
Assertion (assertions): Assertion (assertions) Is the core part of the test method , It is used to verify the conditions that the test needs to meet . These assertion methods are org.junit.jupiter.api.Assertions Static method of .JUnit 5 Built in assertions can be divided into the following categories :
Check whether the data returned by the business logic is reasonable .
After all the test runs , There will be a detailed test report ;
1、 Simple assertion
Used for simple validation of a single value . Such as :
Method explain
assertEquals Determine whether two objects or two primitive types are equal
assertNotEquals Determine whether two objects or two primitive types are not equal
assertSame Determine whether two object references point to the same object
assertNotSame Judge whether two object references point to different objects
assertTrue Determines whether the given Boolean value is true
assertFalse Determines whether the given Boolean value is false
assertNull Determine whether the given object reference is null
assertNotNull Determine whether the given object reference is not null
Code implementation :
@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、 Array assertion
adopt assertArrayEquals Method to determine whether two objects or arrays of primitive types are equal
@Test
@DisplayName("array assertion")
public void array() {
assertArrayEquals(new int[]{
1, 2}, new int[] {
1, 2});
}
3、 Combine assertions
assertAll Method accepts multiple org.junit.jupiter.api.Executable An instance of a functional interface as an assertion to verify , Can pass lambda Expressions can easily provide these assertions
@Test
@DisplayName("assert all")
public void all() {
assertAll("Math",
() -> assertEquals(2, 1 + 1),
() -> assertTrue(1 > 0)
);
}
4、 Exception assertion
stay JUnit4 period , When you want to test the exception of a method , Need to use @Rule Annotated ExpectedException Variables are still troublesome . and JUnit5 Provides a new assertion method Assertions.assertThrows() , It can be used with functional programming .
@Test
@DisplayName(" Abnormal test ")
public void exceptionTest() {
ArithmeticException exception = Assertions.assertThrows(
// Throw assertion exception
ArithmeticException.class, () -> System.out.println(1 % 0));
}
5、 Assertion timeout
Junit5 It also provides Assertions.assertTimeout() Set timeout for test method
@Test
@DisplayName(" Timeout tests ")
public void timeoutTest() {
// If the test method takes longer than 1s Will be abnormal
Assertions.assertTimeout(Duration.ofMillis(1000), () -> Thread.sleep(500));
}
6、 Fast failure
adopt fail Method directly causes the test to fail
@Test
@DisplayName("fail")
public void shouldFail() {
fail("This should fail");
}
Assertion is a very important function in our development , In development, we usually write the business logic module before going online , We have to write a unit test class for unit testing . It will give us a complete summary report , What became and what failed , It will give us an accurate positioning .
Use Maven Of test function , Only after the test is successful can we package
边栏推荐
- The share price soared 180.46%! Shanghai silicon industry, the leader of domestic large silicon wafers, is listed: the cumulative net profit in recent four years is less than 60million
- 网站被黑后处理方法及删除批量恶意代码的方法步骤
- 【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
- [C language] address book (dynamic version)
- What are the methods of process synchronization?
- [GWCTF 2019]BabyRSA1
- org.junit.runners.model. InvalidTestClassError: Invalid test class ‘com.zhj.esdemo. MysqlTests‘: 1.
- Redis 哈希Hash底层数据结构
- [RoarCTF2019]babyRSA威尔逊定理
- TFRecord的Shuffle、划分和读取
猜你喜欢

2022 summer vacation daily question (5)

Posture recognition and simple behavior recognition based on mediapipe

BUUCTF-RSA roll

xss.haozi.me练习通关

Spark 离线开发框架设计与实现

The first activity of togaf10 standard reading club was successfully held, and the wonderful moments were reviewed!

Sudden, wechat important notice

字符流学习14.3

TOGAF10标准读书会首场活动圆满举办,精彩时刻回顾!

解密 OOM 崩溃下降 90% 的秘密~
随机推荐
urllib.error. URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: un
Under the epidemic, TSMC's growth in the first quarter exceeded expectations, with 7Nm accounting for 35%! Second quarter or record high
Redis的分布式锁
2022年土木,建筑与环境工程国际会议(ICCAEE 2022)
虚拟存储器与Cache的比较
解密 OOM 崩溃下降 90% 的秘密~
Latex常用总结(2):输入矩阵(输入矩阵、对角阵、方程组等)
J9 Digital Science Popularization: how does the double consensus of Sui network work?
Latex common summary (2): input matrix (input matrix, diagonal matrix, equations, etc.)
Reduce error demonstration
Bank Marketing预测一个客户购买理财产品的成功率
JS promotion: array flattening in JS
硬布线控制器的特点:
Introduction to several common usage scenarios of message queue
Xss.haozi.me practice customs clearance
Flutter pull_ to_ refresh-1.6.0/lib/src/internals/slivers. dart:164:13: Error: Method not found: ‘descr
[RoarCTF2019]RSA
[MRCTF2020]babyRSA
BUUCTF-RSA
smartRefresh嵌套多个RecycleView滑动冲突及布局显示不全