diff --git a/junit5/src/test/java/com/baeldung/RepeatedTestExample.java b/junit5/src/test/java/com/baeldung/RepeatedTestExample.java new file mode 100644 index 0000000000..8674e01e5d --- /dev/null +++ b/junit5/src/test/java/com/baeldung/RepeatedTestExample.java @@ -0,0 +1,52 @@ +package com.baeldung; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.RepetitionInfo; +import org.junit.jupiter.api.TestInfo; + +public class RepeatedTestExample { + + @BeforeEach + void beforeEachTest() { + System.out.println("Before Each Test"); + } + + @AfterEach + void afterEachTest() { + System.out.println("After Each Test"); + System.out.println("====================="); + } + + @RepeatedTest(3) + void repeatedTest(TestInfo testInfo) { + System.out.println("Executing repeated test"); + assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2"); + } + + @RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME) + void repeatedTestWithLongName() { + System.out.println("Executing repeated test with long name"); + assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2"); + } + + @RepeatedTest(value = 3, name = RepeatedTest.SHORT_DISPLAY_NAME) + void repeatedTestWithShortName() { + System.out.println("Executing repeated test with long name"); + assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2"); + } + + @RepeatedTest(value = 3, name = "Custom name {currentRepetition}/{totalRepetitions}") + void repeatedTestWithCustomDisplayName() { + assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2"); + } + + @RepeatedTest(3) + void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) { + System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition()); + assertEquals(3, repetitionInfo.getTotalRepetitions()); + } +}