|
| 1 | +package junit5; |
| 2 | + |
| 3 | +import org.apache.logging.log4j.util.Strings; |
| 4 | +import org.junit.jupiter.api.AfterAll; |
| 5 | +import org.junit.jupiter.api.AfterEach; |
| 6 | +import org.junit.jupiter.api.BeforeAll; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.params.ParameterizedTest; |
| 11 | +import org.junit.jupiter.params.provider.Arg
8000
uments; |
| 12 | +import org.junit.jupiter.params.provider.MethodSource; |
| 13 | + |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 18 | + |
| 19 | +public class Junit5Test { |
| 20 | + @BeforeAll |
| 21 | + static void setup() { |
| 22 | + System.out.println("@BeforeAll - executes once before all test methods in this class"); |
| 23 | + } |
| 24 | + |
| 25 | + @BeforeEach |
| 26 | + void init() { |
| 27 | + System.out.println("@BeforeEach - executes before each test method in this class"); |
| 28 | + } |
| 29 | + |
| 30 | + @DisplayName("Single test successful") |
| 31 | + @Test |
| 32 | + void testSingleSuccessTest() { |
| 33 | + System.out.println("Success"); |
| 34 | + assertEquals(5 + 2, 7); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void shouldThrowException() { |
| 39 | + Throwable exception = assertThrows(UnsupportedOperationException.class, () -> { |
| 40 | + throw new UnsupportedOperationException("Not supported"); |
| 41 | + }); |
| 42 | + assertEquals("Not supported", exception.getMessage()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void assertThrowsException() { |
| 47 | + String str = null; |
| 48 | + assertThrows(IllegalArgumentException.class, () -> { |
| 49 | + Integer.valueOf(str); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + @ParameterizedTest |
| 54 | + @MethodSource("data") |
| 55 | + public void parameterizedTest(String input, boolean expected) { |
| 56 | + assertEquals(expected, Strings.isBlank(input)); |
| 57 | + } |
| 58 | + |
| 59 | + private static Stream<Arguments> data() { |
| 60 | + return Stream.of( |
| 61 | + Arguments.of(null, true), |
| 62 | + Arguments.of("", true), |
| 63 | + Arguments.of(" ", true), |
| 64 | + Arguments.of("not blank", false) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterEach |
| 69 | + void tearDown() { |
| 70 | + System.out.println("@AfterEach - executed after each test method."); |
| 71 | + } |
| 72 | + |
| 73 | + @AfterAll |
| 74 | + static void done() { |
| 75 | + System.out.println("@AfterAll - executed after all test methods."); |
| 76 | + } |
| 77 | +} |
0 commit comments