Skip to content

Commit

Permalink
Polish TestExecutionResultConditions API
Browse files Browse the repository at this point in the history
Issue: #1621
  • Loading branch information
sbrannen committed Dec 14, 2018
1 parent 5d577fa commit b37b48e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static org.junit.platform.testkit.engine.EventConditions.test;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.isA;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.suppressed;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.suppressedThrowable;

import java.io.IOException;
import java.util.Optional;
Expand Down Expand Up @@ -132,7 +132,7 @@ void checkedExceptionInAfterEachIsSuppressedByExceptionInTest() {
finishedWithFailure(allOf( //
isA(RuntimeException.class), //
message("unchecked"), //
suppressed(0, allOf(isA(IOException.class), message("checked")))))), //
suppressedThrowable(0, allOf(isA(IOException.class), message("checked")))))), //
event(container(testClass), finishedSuccessfully()), //
event(engine(), finishedSuccessfully()));
}
Expand All @@ -151,7 +151,7 @@ void exceptionInAfterEachTakesPrecedenceOverFailedAssumptionInTest() {
finishedWithFailure(allOf( //
isA(IOException.class), //
message("checked"), //
suppressed(0, allOf(isA(TestAbortedException.class)))))), //
suppressedThrowable(0, allOf(isA(TestAbortedException.class)))))), //
event(container(FailureTestCase.class), finishedSuccessfully()), //
event(engine(), finishedSuccessfully()));
}
Expand Down Expand Up @@ -197,7 +197,7 @@ void exceptionInAfterAllCallbackDoesNotHideExceptionInBeforeAllCallback() {
event(container(testClass), started()), //
event(container(testClass), finishedWithFailure(allOf( //
message("beforeAll callback"), //
suppressed(0, message("afterAll callback"))))), //
suppressedThrowable(0, message("afterAll callback"))))), //
event(engine(), finishedSuccessfully()));
}

Expand Down Expand Up @@ -237,7 +237,7 @@ void failureInAfterAllTakesPrecedenceOverTestAbortedExceptionInBeforeAll() {
event(container(FailureTestCase.class), started()), //
event(container(FailureTestCase.class),
finishedWithFailure(allOf(isA(IOException.class), message("checked"),
suppressed(0, allOf(isA(TestAbortedException.class), message("aborted")))))), //
suppressedThrowable(0, allOf(isA(TestAbortedException.class), message("aborted")))))), //
event(engine(), finishedSuccessfully()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.isA;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.nestedCause;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.nestedThrowable;

import java.lang.reflect.Field;
import java.util.function.Predicate;
Expand Down Expand Up @@ -83,14 +83,15 @@ void classLevelFromInterface() {

@Test
void propagatesCheckedExceptionThrownDuringInitializationOfStaticField() {
assertClassFails(ClassLevelExplosiveCheckedExceptionTestCase.class,
allOf(isA(ExceptionInInitializerError.class), nestedCause(allOf(isA(Exception.class), message("boom")))));
assertClassFails(ClassLevelExplosiveCheckedExceptionTestCase.class, allOf(
isA(ExceptionInInitializerError.class), nestedThrowable(allOf(isA(Exception.class), message("boom")))));
}

@Test
void propagatesUncheckedExceptionThrownDuringInitializationOfStaticField() {
assertClassFails(ClassLevelExplosiveUncheckedExceptionTestCase.class, allOf(
isA(ExceptionInInitializerError.class), nestedCause(allOf(isA(RuntimeException.class), message("boom")))));
assertClassFails(ClassLevelExplosiveUncheckedExceptionTestCase.class,
allOf(isA(ExceptionInInitializerError.class),
nestedThrowable(allOf(isA(RuntimeException.class), message("boom")))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private static Condition<Event> finishedWithCause(Status expectedStatus,
Condition<? super Throwable> causeCondition) {

return finished(Assertions.allOf(TestExecutionResultConditions.status(expectedStatus),
TestExecutionResultConditions.cause(causeCondition)));
TestExecutionResultConditions.throwable(causeCondition)));
}

public static Condition<Event> finishedWithFailure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ public static Condition<TestExecutionResult> status(Status expectedStatus) {
expectedStatus);
}

public static Condition<TestExecutionResult> cause(Condition<? super Throwable> condition) {
public static Condition<TestExecutionResult> throwable(Condition<? super Throwable> condition) {
return new Condition<>(
where(TestExecutionResult::getThrowable,
throwable -> throwable.isPresent() && condition.matches(throwable.get())),
"cause matches %s", condition);
"throwable matches %s", condition);
}

public static Condition<Throwable> nestedCause(Condition<Throwable> condition) {
return new Condition<>(throwable -> condition.matches(throwable.getCause()), "nested cause matches %s",
public static Condition<Throwable> nestedThrowable(Condition<Throwable> condition) {
return new Condition<>(throwable -> condition.matches(throwable.getCause()), "nested throwable matches %s",
condition);
}

public static Condition<Throwable> suppressed(int index, Condition<Throwable> condition) {
public static Condition<Throwable> suppressedThrowable(int index, Condition<Throwable> condition) {
return new Condition<>(
throwable -> throwable.getSuppressed().length > index
&& condition.matches(throwable.getSuppressed()[index]),
"suppressed exception at index %d matches %s", index, condition);
"suppressed throwable at index %d matches %s", index, condition);
}

public static Condition<Throwable> isA(Class<? extends Throwable> expectedType) {
Expand All @@ -69,7 +69,7 @@ public static Condition<Throwable> message(String expectedMessage) {
}

public static Condition<Throwable> message(Predicate<String> expectedMessagePredicate) {
return new Condition<>(where(Throwable::getMessage, expectedMessagePredicate), "message predicate");
return new Condition<>(where(Throwable::getMessage, expectedMessagePredicate), "message matches predicate");
}

}

0 comments on commit b37b48e

Please sign in to comment.