public final class ExpectFailure extends Object
Subject fail when they should,
plus a utility to assert about parts of the resulting failure messages.
Usage:
AssertionError failure =
expectFailure(whenTesting -> whenTesting.that(cancelButton).isVisible());
assertThat(failure).factKeys().containsExactly("expected to be visible");
...
private static AssertionError expectFailure(
ExpectFailure.SimpleSubjectBuilderCallback<UiElementSubject, UiElement> assertionCallback) {
return ExpectFailure.expectFailureAbout(uiElements(), assertionCallback);
}
Or, if you can't use lambdas:
@Rule public final ExpectFailure expectFailure = new ExpectFailure();... expectFailure.whenTesting().about(uiElements()).that(cancelButton).isVisible(); assertThat(failure).factKeys().containsExactly("expected to be visible");
ExpectFailure is similar to JUnit 5's assertThrows. We recommend it over assertThrows when you're testing a Truth subject
because it also checks that the assertion you're testing correctly calls FailureStrategy.fail(java.lang.AssertionError) only once.
| Modifier and Type | Class and Description |
|---|---|
static interface |
ExpectFailure.SimpleSubjectBuilderCallback<S extends Subject<S,A>,A>
A "functional interface" for
expectFailureAbout() to invoke and
capture failures. |
static interface |
ExpectFailure.StandardSubjectBuilderCallback
A "functional interface" for
expectFailure() to invoke and capture
failures. |
| Constructor and Description |
|---|
ExpectFailure()
Creates a new instance for use as a
@Rule. |
| Modifier and Type | Method and Description |
|---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description) |
static TruthFailureSubject |
assertThat(AssertionError actual)
Creates a subject for asserting about the given
AssertionError, usually one produced by
Truth. |
static AssertionError |
expectFailure(ExpectFailure.StandardSubjectBuilderCallback assertionCallback)
Static alternative that directly returns the triggered failure.
|
static <S extends Subject<S,A>,A> |
expectFailureAbout(Subject.Factory<S,A> factory,
ExpectFailure.SimpleSubjectBuilderCallback<S,A> assertionCallback)
Static alternative that directly returns the triggered failure.
|
AssertionError |
getFailure()
Returns the captured failure, if one occurred.
|
StandardSubjectBuilder |
whenTesting()
Returns a test verb that expects the chained assertion to fail, and makes the failure available
via
getFailure(). |
public ExpectFailure()
@Rule. See the class documentation for details, and
consider using the lambda version instead.public StandardSubjectBuilder whenTesting()
getFailure().
An instance of ExpectFailure supports only one whenTesting call per test
method. The static expectFailure(com.google.common.truth.ExpectFailure.StandardSubjectBuilderCallback) method, by contrast, does not have this limitation.
public AssertionError getFailure()
public static AssertionError expectFailure(ExpectFailure.StandardSubjectBuilderCallback assertionCallback)
expectThrows():
AssertionError failure = expectFailure(whenTesting ->
whenTesting.that(4).isNotEqualTo(4));
public static <S extends Subject<S,A>,A> AssertionError expectFailureAbout(Subject.Factory<S,A> factory, ExpectFailure.SimpleSubjectBuilderCallback<S,A> assertionCallback)
expectThrows():
AssertionError failure = expectFailureAbout(myTypes(), whenTesting ->
whenTesting.that(myType).hasProperty());
public static TruthFailureSubject assertThat(AssertionError actual)
AssertionError, usually one produced by
Truth.@GwtIncompatible(value="org.junit.rules.TestRule") public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
Copyright © 2019. All rights reserved.