Skip to content

Commit

Permalink
Exceptions in @AfterEach methods fail aborted tests
Browse files Browse the repository at this point in the history
Issue: #1313
  • Loading branch information
marcphilipp committed Jul 7, 2018
1 parent 3b3ea3c commit 26a964a
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.jupiter.api.TestInstance.Lifecycle.PER_METHOD;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
Expand Down Expand Up @@ -44,6 +45,7 @@
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.engine.test.event.ExecutionEventRecorder;
import org.opentest4j.AssertionFailedError;
import org.opentest4j.TestAbortedException;

/**
* Integration tests that verify correct exception handling in the {@link JupiterTestEngine}.
Expand Down Expand Up @@ -146,6 +148,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 {
Class<?> testClass = FailureTestCase.class;
Expand Down Expand Up @@ -283,6 +307,11 @@ void testWithCheckedException() throws IOException {
throw new IOException("checked");
}

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

}

@TestInstance(PER_METHOD)
Expand Down

0 comments on commit 26a964a

Please sign in to comment.