|
| 1 | +package com.baeldung; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.AfterEach; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.RepeatedTest; |
| 8 | +import org.junit.jupiter.api.RepetitionInfo; |
| 9 | +import org.junit.jupiter.api.TestInfo; |
| 10 | + |
| 11 | +public class RepeatedTestExample { |
| 12 | + |
| 13 | + @BeforeEach |
| 14 | + void beforeEachTest() { |
| 15 | + System.out.println("Before Each Test"); |
| 16 | + } |
| 17 | + |
| 18 | + @AfterEach |
| 19 | + void afterEachTest() { |
| 20 | + System.out.println("After Each Test"); |
| 21 | + System.out.println("====================="); |
| 22 | + } |
| 23 | + |
| 24 | + @RepeatedTest(3) |
| 25 | + void repeatedTest(TestInfo testInfo) { |
| 26 | + System.out.println("Executing repeated test"); |
| 27 | + assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2"); |
| 28 | + } |
| 29 | + |
| 30 | + @RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME) |
| 31 | + void repeatedTestWithLongName() { |
| 32 | + System.out.println("Executing repeated test with long name"); |
| 33 | + assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2"); |
| 34 | + } |
| 35 | + |
| 36 | + @RepeatedTest(value = 3, name = RepeatedTest.SHORT_DISPLAY_NAME) |
| 37 | + void repeatedTestWithShortName() { |
| 38 | + System.out.println("Executing repeated test with long name"); |
| 39 | + assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2"); |
| 40 | + } |
| 41 | + |
| 42 | + @RepeatedTest(value = 3, name = "Custom name {currentRepetition}/{totalRepetitions}") |
| 43 | + void repeatedTestWithCustomDisplayName() { |
| 44 | + assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2"); |
| 45 | + } |
| 46 | + |
| 47 | + @RepeatedTest(3) |
| 48 | + void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) { |
| 49 | + System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition()); |
| 50 | + assertEquals(3, repetitionInfo.getTotalRepetitions()); |
| 51 | + } |
| 52 | +} |
0 commit comments