Skip to content

Commit 47e687e

Browse files
committed
Update FailureDetectingExternalResource to suppress post-test failures if the test fails
1 parent ca5f5ae commit 47e687e

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

modules/junit-vintage/src/main/java/org/testcontainers/junit/vintage/FailureDetectingExternalResource.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.util.ArrayList;
1010
import java.util.List;
11+
import java.util.Optional;
1112

1213
/**
1314
* {@link TestRule} which is called before and after each test, and also is notified on success/failure.
@@ -23,18 +24,20 @@ public final Statement apply(Statement base, Description description) {
2324
@Override
2425
public void evaluate() throws Throwable {
2526
List<Throwable> errors = new ArrayList<Throwable>();
27+
Optional<Throwable> failure = Optional.empty();
2628

2729
try {
2830
starting(description);
2931
base.evaluate();
3032
notifySucceeded(description, errors);
3133
} catch (org.junit.internal.AssumptionViolatedException e) {
32-
// Do nothing.
34+
failure = Optional.of(e);
3335
} catch (Throwable e) {
36+
failure = Optional.of(e);
3437
errors.add(e);
35-
notifyFailed(e, description, errors);
38+
notifyFailed(e, description);
3639
} finally {
37-
notifyFinished(description, errors);
40+
notifyFinished(failure, description, errors);
3841
}
3942

4043
MultipleFailureException.assertEmpty(errors);
@@ -46,9 +49,9 @@ protected void starting(Description description) throws Throwable {}
4649

4750
protected void succeeded(Description description) throws Throwable {}
4851

49-
protected void failed(Throwable e, Description description) {}
52+
protected void failed(Throwable e, Description description) throws Throwable {}
5053

51-
protected void finished(Description description, List<Throwable> errors) {}
54+
protected void finished(Description description) throws Throwable {}
5255

5356
private void notifySucceeded(Description description, List<Throwable> errors) {
5457
try {
@@ -58,19 +61,22 @@ private void notifySucceeded(Description description, List<Throwable> errors) {
5861
}
5962
}
6063

61-
private void notifyFailed(Throwable failure, Description description, List<Throwable> errors) {
64+
private void notifyFailed(Throwable failure, Description description) {
6265
try {
6366
failed(failure, description);
6467
} catch (Throwable e) {
65-
errors.add(e);
68+
failure.addSuppressed(e);
6669
}
6770
}
6871

69-
private void notifyFinished(Description description, List<Throwable> errors) {
72+
private void notifyFinished(Optional<Throwable> failure, Description description, List<Throwable> errors) {
7073
try {
71-
finished(description, errors);
74+
finished(description);
7275
} catch (Throwable e) {
73-
errors.add(e);
76+
failure.ifPresent(f -> f.addSuppressed(e)); // ifPresentOrElse() requires Java 9
77+
if (!failure.isPresent()) {
78+
errors.add(e);
79+
}
7480
}
7581
}
7682

modules/junit-vintage/src/main/java/org/testcontainers/junit/vintage/Testcontainers.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.platform.commons.support.ModifierSupport;
66
import org.junit.platform.commons.support.ReflectionSupport;
77
import org.junit.runner.Description;
8+
import org.junit.runners.model.MultipleFailureException;
89
import org.testcontainers.lifecycle.Startable;
910
import org.testcontainers.lifecycle.TestDescription;
1011
import org.testcontainers.lifecycle.TestLifecycleAware;
@@ -97,7 +98,9 @@ protected void failed(Throwable e, Description description) {
9798
}
9899

99100
@Override
100-
protected void finished(Description description, List<Throwable> errors) {
101+
protected void finished(Description description) throws Exception {
102+
List<Throwable> errors = new ArrayList<Throwable>();
103+
101104
forEachReversed(
102105
startedContainers,
103106
startable -> {
@@ -108,6 +111,8 @@ protected void finished(Description description, List<Throwable> errors) {
108111
}
109112
}
110113
);
114+
115+
MultipleFailureException.assertEmpty(errors);
111116
}
112117

113118
private List<Startable> findContainers(Description description) {

0 commit comments

Comments
 (0)