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); } }
//不相等抛出异常 @Test public void array() { assertArrayEquals(new int[]{1, 2}, new int[] {1, 2, 3}); }
//除非填写的断言都为真, 否则抛出异常 @Test public void all() { assertAll("Math", () -> assertEquals(2, 1 + 1), () -> assertTrue(1 > 5), () -> assertNotNull(null) ); }
@Test public void exceptionTest() { ArithmeticException exception = Assertions.assertThrows( //当没有异常时, 扔出断言异常 ArithmeticException.class, () -> System.out.println(5/ 2)); }
@Test@DisplayName("超时测试")public void timeoutTest() { //如果测试方法执行时间超过设置的时间将会抛出异常 Assertions.assertTimeout(Duration.ofMillis(1000), () -> Thread.sleep(5000)); }
@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!