File tree Expand file tree Collapse file tree 6 files changed +134
-1
lines changed
testing-modules/junit-5-advanced-2
src/test/java/com/baeldung/retryjunit Expand file tree Collapse file tree 6 files changed +134
-1
lines changed Original file line number Diff line number Diff line change 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 >
6181 <system-lambda .version>1.2.1</system-lambda .version>
6282 </properties >
6383
64- </project >
84+ </project >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments