...

/

Repeated Tests

Repeated Tests

Learn how to use the @RepeatedTest annotation in JUnit 5.

Using repeated tests

We may occasionally want to run a test method multiple times. JUnit 5 has the org.junit.jupiter.api.RepeatedTest annotation to easily support this requirement. We simply need to use @RepeatedTest instead of @Test to annotate methods and specify the total number of repetitions. Each repetition of the repeated test behaves like a regular test method with @Test.

Press + to interact
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
@DisplayName("TestReporter")
public class ReporterTest {
@RepeatedTest(3)
@DisplayName("simple")
void simpleTest() {
}
@RepeatedTest(3)
@DisplayName("a little complex")
void testWithRepititionInfo(RepetitionInfo repetitionInfo) {
assertNotEquals(2, repetitionInfo.getCurrentRepetition());
}
}

In the code above:

  • Lines 6–9: The
...