Skip to content

Commit da53793

Browse files
authored
How to Implement Retry for JUnit Tests (#18527)
* feat: added test codes Signed-off-by: Emmanuel Mireku Omari <[email protected]> * fix: added indentation to dependancies Signed-off-by: Emmanuel Mireku Omari <[email protected]> * fix: used 4-space indentation instead of tabs Signed-off-by: Emmanuel Mireku Omari <[email protected]> * fix: updated module and indents Signed-off-by: Emmanuel Mireku Omari <[email protected]> --------- Signed-off-by: Emmanuel Mireku Omari <[email protected]>
1 parent a64ac4d commit da53793

File tree

6 files changed

+134
-1
lines changed

6 files changed

+134
-1
lines changed

testing-modules/junit-5-advanced-2/pom.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@
3333
<version>${system-lambda.version}</version>
3434
<scope>test</scope>
3535
</dependency>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-api</artifactId>
39+
<version>5.10.0</version>
40+
<scope>test</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.junit.jupiter</groupId>
45+
<artifactId>junit-jupiter-engine</artifactId>
46+
<version>5.10.0</version>
47+
<scope>test</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.junit-pioneer</groupId>
52+
<artifactId>junit-pioneer</artifactId>
53+
<version>2.0.1</version>
54+
<scope>test</scope>
55+
</dependency>
3656
</dependencies>
3757

3858
<build>
@@ -61,4 +81,4 @@
6181
<system-lambda.version>1.2.1</system-lambda.version>
6282
</properties>
6383

64-
</project>
84+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.retryjunit;
2+
3+
import org.junit.jupiter.api.extension.ExtensionContext;
4+
import org.junit.jupiter.api.extension.ExtensionContext.Store;
5+
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
6+
7+
public class RetryExtension implements TestExecutionExceptionHandler {
8+
private static final int MAX_RETRIES = 3;
9+
private static final ExtensionContext.Namespace NAMESPACE =
10+
ExtensionContext.Namespace.create("RetryExtension");
11+
12+
@Override
13+
public void handleTestExecutionException(ExtensionContext context, Throwable throwable)
14+
throws Throwable {
15+
Store store = context.getStore(NAMESPACE);
16+
int retries = store.getOrDefault("retries", Integer.class, 0);
17+
18+
if (retries < MAX_RETRIES) {
19+
retries++;
20+
store.put("retries", retries);
21+
System.out.println("Retrying test " + context.getDisplayName() + ", attempt " + retries);
22+
throw throwable;
23+
} else {
24+
throw throwable;
25+
}
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.retryjunit;
2+
3+
import org.junitpioneer.jupiter.RetryingTest;
4+
5+
public class RetryPioneerTest {
6+
private static int attempt = 0;
7+
8+
@RetryingTest(maxAttempts = 3)
9+
void testWithRetry() {
10+
attempt++;
11+
System.out.println("Test attempt: " + attempt);
12+
if (attempt < 3) {
13+
throw new RuntimeException("Failing test");
14+
}
15+
}
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.retryjunit;
2+
3+
import org.junit.rules.TestRule;
4+
import org.junit.runner.Description;
5+
import org.junit.runners.model.Statement;
6+
7+
public class RetryRule implements TestRule {
8+
private final int retryCount;
9+
10+
public RetryRule(int retryCount) {
11+
this.retryCount = retryCount;
12+
}
13+
14+
@Override
15+
public Statement apply(Statement base, Description description) {
16+
return new Statement() {
17+
@Override
18+
public void evaluate() throws Throwable {
19+
Throwable failure = null;
20+
for (int i = 0; i < retryCount; i++) {
21+
try {
22+
base.evaluate();
23+
return;
24+
} catch (Throwable t) {
25+
failure = t;
26+
System.out.println("Retry " + (i + 1) + "/" + retryCount + " for test " + description.getDisplayName());
27+
}
28+
}
29+
throw failure;
30+
}
31+
};
32+
}
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.retryjunit;
2+
3+
import org.junit.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
5+
6+
@ExtendWith(RetryExtension.class)
7+
public class RetryTest {
8+
private static int attempt = 0;
9+
10+
@Test
11+
public void testWithRetry() {
12+
attempt++;
13+
System.out.println("Test attempt: " + attempt);
14+
if (attempt < 3) {
15+
throw new RuntimeException("Failing test");
16+
}
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.retryjunit;
2+
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
6+
public class RetryTestJUnit4 {
7+
@Rule
8+
public RetryRule retryRule = new RetryRule(3);
9+
private static int attempt = 0;
10+
11+
@Test
12+
public void testWithRetry() {
13+
attempt++;
14+
System.out.println("Test attempt: " + attempt);
15+
if (attempt < 3) {
16+
throw new RuntimeException("Failing test");
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)