Skip to content

Commit 98cef28

Browse files
Avoid skipping the last error for @RetryingTest (#820 / #821)
Fixes @RetryingTest "eating" the last exception for a test that always fails. Closes: #820
1 parent e9e465e commit 98cef28

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

README.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ The least we can do is to thank them and list some of their accomplishments here
124124
==== 2024
125125

126126
* https://github.com/TWiStErRob[Papp Róbert (TWiStErRob)] updated Gradle and GitHub Actions tooling to latest. (#803 / #804 / #805)
127+
* https://github.com/boris-faniuk-n26[Boris Faniuk] contributed to `@RetryingTest` extension (#821)
127128

128129
==== 2023
129130

src/main/java/org/junitpioneer/jupiter/RetryingTestExtension.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,16 @@ <E extends Throwable> void failed(E exception) throws E {
163163
exception);
164164
seenExceptions.add(testAbortedException);
165165
throw testAbortedException;
166-
} else
166+
} else {
167+
var testAbortedException = new TestAbortedException(
168+
format("%s%nTest execution #%d (of up to %d) failed ~> will not retry any more",
169+
exception.getMessage(), retriesSoFar, maxRetries),
170+
exception);
171+
seenExceptions.add(testAbortedException);
167172
throw new MultipleFailuresError(format(
168173
"Test execution #%d (of up to %d with at least %d successes) failed ~> test fails - see cause for details",
169174
retriesSoFar, maxRetries, minSuccess), seenExceptions);
175+
}
170176
}
171177

172178
private boolean expectedException(Throwable exception) {

src/test/java/org/junitpioneer/jupiter/RetryingTestExtensionTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424

2525
import org.assertj.core.api.Assertions;
26+
import org.assertj.core.api.InstanceOfAssertFactories;
2627
import org.junit.jupiter.api.Test;
2728
import org.junit.jupiter.api.TestInfo;
2829
import org.junit.jupiter.api.TestInstance;
@@ -31,6 +32,8 @@
3132
import org.junit.platform.testkit.engine.Execution;
3233
import org.junitpioneer.testkit.ExecutionResults;
3334
import org.junitpioneer.testkit.PioneerTestKit;
35+
import org.opentest4j.MultipleFailuresError;
36+
import org.opentest4j.TestAbortedException;
3437

3538
class RetryingTestExtensionTests {
3639

@@ -111,6 +114,7 @@ void failsOnlyOnFirstInvocationWithUnexpectedException_executedOnce_fails() {
111114
.executeTestMethod(RetryingTestTestCases.class, "failsOnlyOnFirstInvocationWithUnexpectedException");
112115

113116
assertThat(results).hasNumberOfDynamicallyRegisteredTests(1).hasNumberOfFailedTests(1);
117+
assertThat(results).hasSingleFailedTest().withExceptionInstanceOf(NullPointerException.class);
114118
}
115119

116120
@Test
@@ -122,6 +126,8 @@ void failsOnlyOnFirstInvocationWithUnexpectedException_executedTwice_fails() {
122126
.hasNumberOfDynamicallyRegisteredTests(2)
123127
.hasNumberOfAbortedTests(1)
124128
.hasNumberOfFailedTests(1);
129+
130+
assertThat(results).hasSingleFailedTest().withExceptionInstanceOf(NullPointerException.class);
125131
}
126132

127133
@Test
@@ -132,6 +138,8 @@ void failsAlways_executedThreeTimes_fails() {
132138
.hasNumberOfDynamicallyRegisteredTests(3)
133139
.hasNumberOfAbortedTests(2)
134140
.hasNumberOfFailedTests(1);
141+
142+
assertFailedTest(results);
135143
}
136144

137145
@Test
@@ -184,6 +192,9 @@ void executesOnceWithThreeFails_fails() {
184192
.hasNumberOfAbortedTests(2)
185193
.hasNumberOfFailedTests(1)
186194
.hasNumberOfSucceededTests(1);
195+
196+
assertFailedTest(results);
197+
187198
}
188199

189200
@Test
@@ -195,6 +206,8 @@ void failsThreeTimes_fails() {
195206
.hasNumberOfAbortedTests(2)
196207
.hasNumberOfFailedTests(1)
197208
.hasNumberOfSucceededTests(0);
209+
210+
assertFailedTest(results);
198211
}
199212

200213
@Test
@@ -279,6 +292,7 @@ void failThreeTimesWithSuspend() {
279292
.hasNumberOfSucceededTests(0);
280293

281294
assertSuspendedFor(results, SUSPEND_FOR);
295+
assertFailedTest(results);
282296
}
283297

284298
@Test
@@ -293,6 +307,7 @@ void failThreeTimesWithoutSuspend() {
293307
.hasNumberOfSucceededTests(0);
294308

295309
assertSuspendedFor(results, 0);
310+
assertFailedTest(results);
296311
}
297312

298313
private void assertSuspendedFor(ExecutionResults results, long greaterThanOrEqualTo) {
@@ -315,6 +330,17 @@ private void assertSuspendedFor(ExecutionResults results, long greaterThanOrEqua
315330
}
316331
}
317332

333+
private void assertFailedTest(ExecutionResults results) {
334+
assertThat(results)
335+
.hasSingleFailedTest()
336+
.withExceptionInstanceOf(MultipleFailuresError.class)
337+
.extracting(MultipleFailuresError::getFailures, InstanceOfAssertFactories.list(Throwable.class))
338+
.hasSize(3)
339+
.hasOnlyElementsOfType(TestAbortedException.class)
340+
.extracting(Throwable::getCause)
341+
.hasOnlyElementsOfType(IllegalArgumentException.class);
342+
}
343+
318344
// TEST CASES -------------------------------------------------------------------
319345

320346
// Some tests require state to keep track of the number of test executions.

0 commit comments

Comments
 (0)