Junit 5 @RepeatedTest

there might be a scnerio where we need to repeat a test multiple times. to achieve this, junit 5 provides us @RepeatedTest annotation, we can pass the desired repetation as anotation parameter. If we are using @RepeatedTest then we dont need to paas @Test annotation.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) {
        return appleBag1 + appleBag2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.RepeatedTest;

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

class AppleCalculatorTest {
    @RepeatedTest(5)
    void addAppleTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Each repetition behaves like a separate test case and follows the entire JUnit 5 test case life cycle.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) {
        return appleBag1 + appleBag2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;

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

class AppleCalculatorTest {
    @RepeatedTest(5)
    void addAppleTest() {
        System.out.println("AppleCalculatorTest.addAppleTest");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

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

    @AfterEach
    void tearDown() {
        System.out.println("AppleCalculatorTest.tearDown");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

we can use currentRepetition and totalRepetitions in display name as well.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) {
        return appleBag1 + appleBag2;
    }
}
package com.example.junit5.sujan;

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

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

class AppleCalculatorTest {
    @RepeatedTest(value = 5, name = "{displayName} {currentRepetition} of {totalRepetitions}")
    @DisplayName("Repetition")
    void addAppleTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Junit 5 provides us some predefined name parameters as well.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) {
        return appleBag1 + appleBag2;
    }
}
package com.example.junit5.sujan;

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

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

class AppleCalculatorTest {
    @RepeatedTest(value = 5, name = LONG_DISPLAY_NAME)
    @DisplayName("Long Display Name")
    void addAppleTestCase1() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @RepeatedTest(value = 5, name = CURRENT_REPETITION_PLACEHOLDER)
    @DisplayName("Current Repetition Placeholder")
    void addAppleTestCase2() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @RepeatedTest(value = 5, name = DISPLAY_NAME_PLACEHOLDER)
    @DisplayName("Display Name Placeholder")
    void addAppleTestCase3() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @RepeatedTest(value = 5, name = SHORT_DISPLAY_NAME)
    @DisplayName("Short Display Name")
    void addAppleTestCase4() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @RepeatedTest(value = 5, name = TOTAL_REPETITIONS_PLACEHOLDER)
    @DisplayName("Total Repetitions Placeholder")
    void addAppleTestCase5() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

Repetition Info

Junit provides RepetitionInfo metadata, which is used to retrieve information about current repetition and the total number of repetitions configured in @RepeatedTest.

package org.wesome.junit5;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) {
        return appleBag1 + appleBag2;
    }
}
package org.wesome.junit5;

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

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

class AppleCalculatorTest {
    @RepeatedTest(5)
    void addAppleTest(RepetitionInfo repetitionInfo) {
        AppleCalculator appleCalculator = new AppleCalculator();
        System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition() + " of " + repetitionInfo.getTotalRepetitions());
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

follow us on