当前位置:网站首页>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
边栏推荐
- BUUCTF-childRSA费马小定理
- Error:svn: E155010: ‘/Users/.../Desktop/wrokspace/xxx‘ is scheduled for addition, but is missing
- How to use FTP to realize automatic update of WinForm
- [MRCTF2020]babyRSA
- [NCTF2019]babyRSA1
- [NCTF2019]babyRSA1
- The 4-hour order exceeds 20000+, claiming to be "the most luxurious in a million". Is the domestic brand floating?
- Under the epidemic, TSMC's growth in the first quarter exceeded expectations, with 7Nm accounting for 35%! Second quarter or record high
- C # delegate usage -- console project, which implements events through delegation
- Elk log analysis system installation and deployment
猜你喜欢

Redis hash underlying data structure

Use of date

【zer0pts CTF 2022】 Anti-Fermat

2022 International Conference on civil, building and Environmental Engineering (iccaee 2022)

【C语言】通讯录(动态版本)

Lua basic grammar learning

Realization of gobang man-machine combat

BUUCTF-RSA roll

Realize today's news website based on native JS

BUUCTF-RSA4
随机推荐
UE4 official AEC blueprint case course learning notes
Latex中如何加粗字体 & 如何打出圆圈序号
BUUCTF-RSA4
Current situation and future of Nb IOT industry: cross the threshold of 100million shipments and rush to 5g connection!
Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
JUC toolkit learning
Why do I need to wait for 2msl?
How Flink uses savepoint
The txt file named according to the sequence number is renamed from the back to the front
Ideas, methods and steps of making folding fans with 3DMAX
传奇外网架设教程带图文解说——Gom引擎
2022 International Conference on civil, building and Environmental Engineering (iccaee 2022)
Those "experiences and traps" in the data center
How to bold font in Latex & how to make circle serial number
What are the methods of process synchronization?
JS提升:JS中的数组扁平化问题
Realization of gobang man-machine combat
【开发教程9】疯壳·开源蓝牙心率防水运动手环-心率监测
[RoarCTF2019]RSA
Technical certification | Tupo software and Huawei cloud create a new situation of win-win cooperation