Skip to content

Commit

Permalink
Polish AssertJ Conditions in Test Kit
Browse files Browse the repository at this point in the history
Issue: #1621
  • Loading branch information
sbrannen committed Dec 13, 2018
1 parent 48bf76f commit 777338f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.hasCause;
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 java.lang.reflect.Field;
import java.util.function.Predicate;
Expand Down Expand Up @@ -84,13 +84,13 @@ void classLevelFromInterface() {
@Test
void propagatesCheckedExceptionThrownDuringInitializationOfStaticField() {
assertClassFails(ClassLevelExplosiveCheckedExceptionTestCase.class,
allOf(isA(ExceptionInInitializerError.class), hasCause(allOf(isA(Exception.class), message("boom")))));
allOf(isA(ExceptionInInitializerError.class), nestedCause(allOf(isA(Exception.class), message("boom")))));
}

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
* Collection of AssertJ {@linkplain Condition conditions} for {@link Event}.
*
* @since 1.4
* @see TestExecutionResultConditions
*/
@API(status = EXPERIMENTAL, since = "1.4")
public class EventConditions {
public final class EventConditions {

private EventConditions() {
/* no-op */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
* {@link TestExecutionResult}.
*
* @since 1.4
* @see EventConditions
*/
@API(status = EXPERIMENTAL, since = "1.4")
public class TestExecutionResultConditions {
public final class TestExecutionResultConditions {

private TestExecutionResultConditions() {
/* no-op */
Expand All @@ -39,32 +40,36 @@ public static Condition<TestExecutionResult> status(Status expectedStatus) {
expectedStatus);
}

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

public static Condition<Throwable> message(Predicate<String> predicate) {
return new Condition<>(where(Throwable::getMessage, predicate), "message is \"%s\"", predicate);
public static Condition<Throwable> nestedCause(Condition<Throwable> condition) {
return new Condition<>(throwable -> condition.matches(throwable.getCause()), "nested cause matches %s",
condition);
}

public static Condition<Throwable> isA(Class<? extends Throwable> expectedClass) {
return new Condition<>(expectedClass::isInstance, "instance of %s", expectedClass.getName());
public static Condition<Throwable> suppressed(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);
}

public static Condition<Throwable> suppressed(int index, Condition<Throwable> checked) {
return new Condition<>(
throwable -> throwable.getSuppressed().length > index && checked.matches(throwable.getSuppressed()[index]),
"suppressed at index %d matches %s", index, checked);
public static Condition<Throwable> isA(Class<? extends Throwable> expectedType) {
return new Condition<>(expectedType::isInstance, "instance of %s", expectedType.getName());
}

public static Condition<Throwable> hasCause(Condition<Throwable> checked) {
return new Condition<>(throwable -> checked.matches(throwable.getCause()), "cause matches %s", checked);
public static Condition<Throwable> message(String expectedMessage) {
return new Condition<>(where(Throwable::getMessage, isEqual(expectedMessage)), "message is '%s'",
expectedMessage);
}

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

}

0 comments on commit 777338f

Please sign in to comment.