Skip to content

Commit

Permalink
Ensure exceptions in @AfterEach methods fail previously aborted tests
Browse files Browse the repository at this point in the history
Issue: #1313
  • Loading branch information
marcphilipp committed Mar 28, 2018
1 parent e391ac4 commit 53d906d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.junit.jupiter.api.function.Executable;
import org.junit.platform.commons.util.ExceptionUtils;
import org.junit.platform.commons.util.Preconditions;
import org.opentest4j.TestAbortedException;

/**
* Simple component that can be used to collect one or more instances of
Expand Down Expand Up @@ -57,6 +58,10 @@ private void add(Throwable t) {
if (this.throwable == null) {
this.throwable = t;
}
else if (this.throwable instanceof TestAbortedException && !(t instanceof TestAbortedException)) {
t.addSuppressed(this.throwable);
this.throwable = t;
}
else {
this.throwable.addSuppressed(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.assertj.core.api.Assertions.allOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
Expand Down Expand Up @@ -45,6 +46,7 @@
import org.junit.platform.engine.test.event.ExecutionEventRecorder;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.opentest4j.AssertionFailedError;
import org.opentest4j.TestAbortedException;

/**
* Integration tests that verify correct exception handling in the {@link JupiterTestEngine}.
Expand Down Expand Up @@ -152,6 +154,28 @@ void checkedExceptionInAfterEachIsSuppressedByExceptionInTest() throws NoSuchMet
event(engine(), finishedSuccessfully()));
}

@Test
void exceptionInAfterEachTakesPrecedenceOverFailedAssumptionInTest() throws NoSuchMethodException {
Method method = FailureTestCase.class.getDeclaredMethod("abortedTest");
LauncherDiscoveryRequest request = request().selectors(selectMethod(FailureTestCase.class, method)).build();

FailureTestCase.exceptionToThrowInAfterEach = Optional.of(new IOException("checked"));

ExecutionEventRecorder eventRecorder = executeTests(request);

assertRecordedExecutionEventsContainsExactly(eventRecorder.getExecutionEvents(), //
event(engine(), started()), //
event(container(FailureTestCase.class), started()), //
event(test("abortedTest"), started()), //
event(test("abortedTest"), //
finishedWithFailure(allOf( //
isA(IOException.class), //
message("checked"), //
suppressed(0, allOf(isA(TestAbortedException.class)))))), //
event(container(FailureTestCase.class), finishedSuccessfully()), //
event(engine(), finishedSuccessfully()));
}

@Test
void checkedExceptionInBeforeAllIsRegistered() throws NoSuchMethodException {
Method method = FailureTestCase.class.getDeclaredMethod("succeedingTest");
Expand Down Expand Up @@ -264,6 +288,11 @@ void testWithCheckedException() throws IOException {
throw new IOException("checked");
}

@Test
void abortedTest() {
assumeFalse(true, "abortedTest");
}

}

@TestInstance(PER_CLASS)
Expand Down

0 comments on commit 53d906d

Please sign in to comment.