当前位置:网站首页>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
边栏推荐
- Why does redis cluster use reverse proxy? Just read this one
- Features of hardwired controller:
- J9数字科普:Sui网络的双共识是如何工作的?
- 4小时定单破20000+,自称“百万内最豪华”,国产品牌飘了?
- Reduce error demonstration
- Redis hash underlying data structure
- NDK series (6): let's talk about the way and time to register JNI functions
- xss.haozi.me练习通关
- Construction and application of super large scale knowledge map of ants
- Character stream learning 14.3
猜你喜欢

基于mediapipe的姿态识别和简单行为识别

TCP sticking and unpacking problem + Solution
![[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters](/img/05/7029eb1fe36d7ddab2640f07247c81.png)
[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters

TCP的粘包拆包问题+解决方案

Realize today's news website based on native JS

2022年土木,建筑与环境工程国际会议(ICCAEE 2022)

Posture recognition and simple behavior recognition based on mediapipe

Lua basic grammar learning

Introduction to several common usage scenarios of message queue

【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
随机推荐
J9 Digital Science Popularization: how does the double consensus of Sui network work?
Redis distributed lock
Explain the idempotence of distributed system in detail
Current situation and future of Nb IOT industry: cross the threshold of 100million shipments and rush to 5g connection!
[NCTF2019]babyRSA1
C # delegate usage -- console project, which implements events through delegation
数据管理的重点
TFRecord的Shuffle、划分和读取
五子棋人机对战实现
为什么 Redis 集群要使用反向代理? 看这篇就明白了
Posture recognition and simple behavior recognition based on mediapipe
传奇外网架设教程带图文解说——Gom引擎
Technical certification | Tupo software and Huawei cloud create a new situation of win-win cooperation
【zer0pts CTF 2022】 Anti-Fermat
JUC toolkit learning
Nail alarm tool
Construction and application of super large scale knowledge map of ants
Reduce error demonstration
BUU-CTF basic rsa
Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further