From d67d03bb170eb4ec74973ef44d478089ccf5cb84 Mon Sep 17 00:00:00 2001 From: Yasin Date: Wed, 10 May 2017 12:23:28 +0530 Subject: [PATCH] BAEL-899 Guide to @RepeatedTest in Junit 5 (#1815) * BAEL-88 Testing in Spring Boot * BAEL-88 Integration tests fixed. * BAEL-905 Improvement task for spring boot testing * BAEL-905 Improvement task for spring boot testing * BAEL-926 Upgrade Junit 5 dependencies to use M4 * BAEL-899 Guide to @RepeatedTest in Junit 5 --- .../com/baeldung/RepeatedTestExample.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 junit5/src/test/java/com/baeldung/RepeatedTestExample.java 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()); + } +}