Home  >  Article  >  Java  >  What is the principle of SpringBoot assertion mechanism?

What is the principle of SpringBoot assertion mechanism?

王林
王林forward
2023-05-14 12:28:111486browse

JUnit 5's built-in assertions can be divided into the following categories:

1. Simple assertions

are used to perform simple verification of a single value. Such as:

##assertEquals Determine whether two objects or two primitive types are equalassertNotEqualsDetermine whether two objects or two primitive types are not equalassertSameDetermine whether two object references point to the same objectassertNotSameDetermine whether two object references point to different objects assertTrueJudge whether the given Boolean value is trueassertFalseJudge the given Whether the Boolean value is falseassertNull Determines whether the given object reference is nullassertNotNullJudge whether the given object reference is not null
Method Description
JTest5

package com.limi.springboottest2;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
public class JTest5 {
    @Test
    public void simple() {
        assertEquals(3, 1 + 2, "simple math");
        System.out.println(1);
        assertNotEquals(3, 1 + 1);
        System.out.println(2);
        assertNotSame(new Object(), new Object());
        System.out.println(3);
        Object obj = new Object();
        assertSame(obj, obj);
        System.out.println(4);
        assertFalse(3 > 2);
        System.out.println(5);
        assertTrue(1 < 2);
        System.out.println(6);
        assertNull(null);
        System.out.println(7);
        assertNotNull(new Object());
        System.out.println(8);
    }
}

If the assertion is judged to be false, the program will terminate immediately after throwing an exception. The remaining code will not be executed.

What is the principle of SpringBoot assertion mechanism?

2. Array assertion

Use the assertArrayEquals method to determine whether two objects or arrays of primitive types are equal.

	//不相等抛出异常
    @Test
    public void array() {
        assertArrayEquals(new int[]{1, 2}, new int[] {1, 2, 3});
    }

What is the principle of SpringBoot assertion mechanism?

3. Combination assertion

assertAll() method accepts multiple instances of the org.junit.jupiter.api.Executable functional interface as to be verified Assertions can be easily provided through lambda expressions.

	//除非填写的断言都为真, 否则抛出异常
    @Test
    public void all() {
        assertAll("Math",
                () -> assertEquals(2, 1 + 1),
                () -> assertTrue(1 > 5),
                () -> assertNotNull(null)
        );
    }

What is the principle of SpringBoot assertion mechanism?

Note that if the second assertion is false, the third assertion will still be executed, which means that all assertions filled in assertAll() method b will be executed.

4. Exception assertion

In the JUnit 4 era, when you want to test the abnormal situation of a method, it is still troublesome to use the ExpectedException variable annotated with @Rule. JUnit5 provides a new assertion method, Assertions.assertThrows(), which can be used with functional programming.

    @Test
    public void exceptionTest() {
        ArithmeticException exception = Assertions.assertThrows(
                //当没有异常时, 扔出断言异常
                ArithmeticException.class, () -> System.out.println(5/ 2));
    }

What is the principle of SpringBoot assertion mechanism?

5. Timeout assertion

JUnit5 also provides Assertions.assertTimeout() to set a timeout for the test method.

    @Test@DisplayName("超时测试")public void timeoutTest() {
        //如果测试方法执行时间超过设置的时间将会抛出异常    
        Assertions.assertTimeout(Duration.ofMillis(1000), () -> Thread.sleep(5000));
    }

What is the principle of SpringBoot assertion mechanism?

6. Fast failure

Fail the test directly through the fail method. The subsequent code of this assertion will not be executed

    @Test
    public void shouldFail() {
        System.out.println("123456");
        fail("This should fail");
        System.out.println("888888");
    }

What is the principle of SpringBoot assertion mechanism?

The above is the detailed content of What is the principle of SpringBoot assertion mechanism?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete