这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion testing-modules/junit-5-advanced-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@
<version>${system-lambda.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -61,4 +81,4 @@
<system-lambda.version>1.2.1</system-lambda.version>
</properties>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.retryjunit;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;

public class RetryExtension implements TestExecutionExceptionHandler {
private static final int MAX_RETRIES = 3;
private static final ExtensionContext.Namespace NAMESPACE =
ExtensionContext.Namespace.create("RetryExtension");

@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable)
throws Throwable {
Store store = context.getStore(NAMESPACE);
int retries = store.getOrDefault("retries", Integer.class, 0);

if (retries < MAX_RETRIES) {
retries++;
store.put("retries", retries);
System.out.println("Retrying test " + context.getDisplayName() + ", attempt " + retries);
throw throwable;
} else {
throw throwable;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.retryjunit;

import org.junitpioneer.jupiter.RetryingTest;

public class RetryPioneerTest {
private static int attempt = 0;

@RetryingTest(maxAttempts = 3)
void testWithRetry() {
attempt++;
System.out.println("Test attempt: " + attempt);
if (attempt < 3) {
throw new RuntimeException("Failing test");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.retryjunit;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class RetryRule implements TestRule {
private final int retryCount;

public RetryRule(int retryCount) {
this.retryCount = retryCount;
}

@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Throwable failure = null;
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (Throwable t) {
failure = t;
System.out.println("Retry " + (i + 1) + "/" + retryCount + " for test " + description.getDisplayName());
}
}
throw failure;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.retryjunit;

import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(RetryExtension.class)
public class RetryTest {
private static int attempt = 0;

@Test
public void testWithRetry() {
attempt++;
System.out.println("Test attempt: " + attempt);
if (attempt < 3) {
throw new RuntimeException("Failing test");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.retryjunit;

import org.junit.Rule;
import org.junit.Test;

public class RetryTestJUnit4 {
@Rule
public RetryRule retryRule = new RetryRule(3);
private static int attempt = 0;

@Test
public void testWithRetry() {
attempt++;
System.out.println("Test attempt: " + attempt);
if (attempt < 3) {
throw new RuntimeException("Failing test");
}
}
}