JUnit 5's built-in assertions can be divided into the following categories:
are used to perform simple verification of a single value. Such as:
Method | Description |
---|---|
Determine whether two objects or two primitive types are equal | |
Determine whether two objects or two primitive types are not equal | |
Determine whether two object references point to the same object | |
Determine whether two object references point to different objects | |
Judge whether the given Boolean value is true | |
Judge the given Whether the Boolean value is false | |
Determines whether the given object reference is null | |
Judge whether the given object reference is not null |
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. 2. Array assertionUse 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}); }3. Combination assertionassertAll() 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) ); }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 assertionIn 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)); }5. Timeout assertionJUnit5 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)); }6. Fast failureFail 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"); }
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!