JUnit 5 Repeated Tests

This article shows you how to use the JUnit 5 @RepeatedTest to repeat a test a specified number of times.

P.S Tested with JUnit 5.5.2

1. @RepeatedTest

1.1 The @RepeatedTest test method is just like a regular @Test method, the same life cycle.

RepeatedSample1Test.java

package com.mkyong.repeated;

import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class RepeatedSample1Test {

    @BeforeAll
    static void beforeAll() {
        System.out.println("beforeAll");
    }

    @AfterAll
    static void afterAll() {
        System.out.println("afterAll");
    }

    @BeforeEach
    void beforeEach() {
        System.out.println("beforeEach");
    }

    @AfterEach
    void afterEach() {
        System.out.println("afterEach");
    }

    // Repeat this test 3 times
    @RepeatedTest(3)
    void math_add_1() {
        System.out.println("Run math_add_1()");
        assertEquals(2, 1 + 1);
    }

    @RepeatedTest(3)
    void math_add_2() {
        System.out.println("Run math_add_2()");
        assertEquals(2, 1 + 1);
    }

}

Output in IDE.

output

Output in console.


beforeAll

beforeEach
Run math_add_1()
afterEach

beforeEach
Run math_add_1()
afterEach

beforeEach
Run math_add_1()
afterEach

beforeEach
Run math_add_2()
afterEach

beforeEach
Run math_add_2()
afterEach

beforeEach
Run math_add_2()
afterEach

afterAll

2. Custom Test Name

2.1 We can configure the name of the @RepeatedTest method.

RepeatedSample2Test.java

package com.mkyong.repeated;

import org.junit.jupiter.api.RepeatedTest;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class RepeatedSample2Test {

    @RepeatedTest(3)
    void math_add_1() {
        System.out.println("Run math_add_1()");
        assertEquals(2, 1 + 1);
    }

    @RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME)
    void math_add_2() {
        System.out.println("Run math_add_2()");
        assertEquals(2, 1 + 1);
    }

    @RepeatedTest(value = 3, name = "{displayName} - ABC - {currentRepetition}/{totalRepetitions}")
    void math_add_3() {
        System.out.println("Run math_add_3()");
        assertEquals(2, 1 + 1);
    }

}

Output in IDE.

output

Note

  • {displayName}: Name of the test method.
  • {currentRepetition}: The current repetition count.
  • {totalRepetitions}: The total number of repetitions.

3. RepetitionInfo

3.1 We also can inject the RepetitionInfo as a parameter and access metadata of the @RepeatedTest.

@RepeatedTest.java

package com.mkyong.repeated;

import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class RepeatedSample3Test {

    @RepeatedTest(3)
    void math_add_4(RepetitionInfo repetitionInfo) {
        System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition());
        assertEquals(3, repetitionInfo.getTotalRepetitions());
    }

}

Output in console.


Repetition #1

Repetition #2

Repetition #3

Download Source Code

$ git clone https://github.com/mkyong/junit-examples
$ cd junit5-examples
$ check src/test/java/com/mkyong/repeated/*.java

References

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar