|
| 1 | +package io.github.dunwu.javalib.junit5; |
| 2 | + |
| 3 | +import io.github.dunwu.javalib.bean.Person; |
| 4 | +import org.junit.jupiter.api.BeforeAll; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import static java.time.Duration.ofMillis; |
| 8 | +import static java.time.Duration.ofMinutes; |
| 9 | +import static org.junit.jupiter.api.Assertions.*; |
| 10 | + |
| 11 | +/** |
| 12 | + * Junit5 断言示例 |
| 13 | + * @author Zhang Peng |
| 14 | + * @date 2018-11-29 |
| 15 | + */ |
| 16 | +class AssertionsTests { |
| 17 | + |
| 18 | + private static Person person; |
| 19 | + |
| 20 | + @BeforeAll |
| 21 | + public static void beforeAll() { |
| 22 | + person = new Person("John", "Doe"); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void standardAssertions() { |
| 27 | + assertEquals(2, 2); |
| 28 | + assertEquals(4, 4, "The optional assertion message is now the last parameter."); |
| 29 | + assertTrue('a' < 'b', () -> "Assertion messages can be lazily evaluated -- " |
| 30 | + + "to avoid constructing complex messages unnecessarily."); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void groupedAssertions() { |
| 35 | + // In a grouped assertion all assertions are executed, and any |
| 36 | + // failures will be reported together. |
| 37 | + assertAll("person", () -> assertEquals("John", person.getFirstName()), |
| 38 | + () -> assertEquals("Doe", person.getLastName())); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void dependentAssertions() { |
| 43 | + // Within a code block, if an assertion fails the |
| 44 | + // subsequent code in the same block will be skipped. |
| 45 | + assertAll("properties", () -> { |
| 46 | + String firstName = person.getFirstName(); |
| 47 | + assertNotNull(firstName); |
| 48 | + |
| 49 | + // Executed only if the previous assertion is valid. |
| 50 | + assertAll("first name", () -> assertTrue(firstName.startsWith("J")), |
| 51 | + () -> assertTrue(firstName.endsWith("n"))); |
| 52 | + }, () -> { |
| 53 | + // Grouped assertion, so processed independently |
| 54 | + // of results of first name assertions. |
| 55 | + String lastName = person.getLastName(); |
| 56 | + assertNotNull(lastName); |
| 57 | + |
| 58 | + // Executed only if the previous assertion is valid. |
| 59 | + assertAll("last name", () -> assertTrue(lastName.startsWith("D")), |
| 60 | + () -> assertTrue(lastName.endsWith("e"))); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void exceptionTesting() { |
| 66 | + Throwable exception = assertThrows(IllegalArgumentException.class, () -> { |
| 67 | + throw new IllegalArgumentException("a message"); |
| 68 | + }); |
| 69 | + assertEquals("a message", exception.getMessage()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void timeoutNotExceeded() { |
| 74 | + // The following assertion succeeds. |
| 75 | + assertTimeout(ofMinutes(2), () -> { |
| 76 | + // Perform task that takes less than 2 minutes. |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void timeoutNotExceededWithResult() { |
| 82 | + // The following assertion succeeds, and returns the supplied object. |
| 83 | + String actualResult = assertTimeout(ofMinutes(2), () -> { |
| 84 | + return "a result"; |
| 85 | + }); |
| 86 | + assertEquals("a result", actualResult); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void timeoutNotExceededWithMethod() { |
| 91 | + // The following assertion invokes a method reference and returns an object. |
| 92 | + String actualGreeting = assertTimeout(ofMinutes(2), AssertionsTests::greeting); |
| 93 | + assertEquals("Hello, World!", actualGreeting); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void timeoutExceeded() { |
| 98 | + // The following assertion fails with an error message similar to: |
| 99 | + // execution exceeded timeout of 10 ms by 91 ms |
| 100 | + assertTimeout(ofMillis(10), () -> { |
| 101 | + // Simulate task that takes more than 10 ms. |
| 102 | + Thread.sleep(100); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void timeoutExceededWithPreemptiveTermination() { |
| 108 | + // The following assertion fails with an error message similar to: |
| 109 | + // execution timed out after 10 ms |
| 110 | + assertTimeoutPreemptively(ofMillis(10), () -> { |
| 111 | + // Simulate task that takes more than 10 ms. |
| 112 | + Thread.sleep(100); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + private static String greeting() { |
| 117 | + return "Hello, World!"; |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments