Junit 5 Pioneer @RepeatFailedTest

Some time code is dependent on external conditions, on which we don't have any control, Test cased based on those conditions may fail, in that case, we should retry test case again before actually marking them failed. To achieve this, JUnit Pioneer provides us @RepeatFailedTest annotation.

package com.example.junit5.sujan;

public class AppleCalculator {
    static int APPLE_COUNT = 0;

    public int getAppleCount() {
        return APPLE_COUNT++;
    }
}
package com.example.junit5.sujan;

import org.junitpioneer.jupiter.RepeatFailedTest;

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

class AppleCalculatorTest {
    @RepeatFailedTest(4)
    void addApple() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(3, appleCalculator.getAppleCount(), "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')
    testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}

JUnit Pioneer @RepeatFailedTest annotation internally calls @Test annotation, so annotating test case method with @Test will throw PreconditionViolationException.

package com.example.junit5.sujan;

public class AppleCalculator {
    static int APPLE_COUNT = 0;

    public int getAppleCount() {
        return APPLE_COUNT++;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.RepeatFailedTest;

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

class AppleCalculatorTest {
    @Test
    @RepeatFailedTest(4)
    void addApple() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(3, appleCalculator.getAppleCount(), "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')
    testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}

 

The @RepeatFailedTest repeat all the failed test cases up to certain limit, and if in any of the iteration test cases validates then in order to pass the test suit, all the above failed test cases will be marked as disabled/ignored.

package com.example.junit5.sujan;

public class AppleCalculator {
    static int APPLE_COUNT = 0;

    public int getAppleCount() {
        return APPLE_COUNT++;
    }
}
package com.example.junit5.sujan;

import org.junitpioneer.jupiter.RepeatFailedTest;

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

class AppleCalculatorTest {
    @RepeatFailedTest(5)
    void addApple() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(3, appleCalculator.getAppleCount(), "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')
    testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}

We need to provide a positive integer value as max retry count in @RepeatFailedTest parameter, @RepeatFailedTest will retry the test case up to count times then marks it as failed, but if test case validates before retrying up to count then the rest of the iterations will not be executed.

package com.example.junit5.sujan;

public class AppleCalculator {
    static int APPLE_COUNT = 0;

    public int getAppleCount() {
        return APPLE_COUNT++;
    }
}
package com.example.junit5.sujan;

import org.junitpioneer.jupiter.RepeatFailedTest;

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

class AppleCalculatorTest {
    @RepeatFailedTest(15)
    void addApple() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(3, appleCalculator.getAppleCount(), "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')
    testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}

follow us on