Class ExpectFailure
java.lang.Object
com.google.common.truth.ExpectFailure
- All Implemented Interfaces:
TestRule
A utility for testing that assertions against a custom
Subject fail when they should,
plus a utility to assert about parts of the resulting failure messages.
Usage:
AssertionError e =
expectFailure(whenTesting -> whenTesting.that(cancelButton).isVisible());
assertThat(e).factKeys().containsExactly("expected to be visible");
...
private static AssertionError expectFailure(
SimpleSubjectBuilderCallback<UiElementSubject, UiElement> assertionCallback) {
return expectFailureAbout(uiElements(), assertionCallback);
}
ExpectFailure also supports a legacy approach, which we no longer recommend now that all
Truth users can use lambdas. That approach is based on the JUnit @Rule system:
@Rule public final ExpectFailure expectFailure = new ExpectFailure();... expectFailure.whenTesting().about(uiElements()).that(cancelButton).isVisible(); assertThat(expectFailure.getFailure()).factKeys().containsExactly("expected to be visible");
ExpectFailure is similar to JUnit's assertThrows (JUnit
4, JUnit
5). We recommend it over assertThrows when you're testing a Truth subject because:
- It performs additional checks:
- It checks that the assertion you're testing uses the supplied
FailureStrategy. - It checks that the assertion you're testing calls
FailureStrategy.fail(java.lang.AssertionError)only once.
- It checks that the assertion you're testing uses the supplied
- It instructs Truth to generate failure messages without adding lines like "value of:
foo()" for
assertThat(foo())....calls that it detects in the test bytecode. Truth doesn't provide guarantees for when such lines will be generated, so tests become more resilient without them.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceExpectFailure.SimpleSubjectBuilderCallback<S extends Subject, A>A functional interface forexpectFailureAbout()to invoke and capture failures.static interfaceA functional interface forexpectFailure()to invoke and capture failures. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapply(Statement base, Description description) static TruthFailureSubjectassertThat(@Nullable AssertionError actual) Creates a subject for asserting about the givenAssertionError, usually one produced by Truth.static AssertionErrorexpectFailure(ExpectFailure.StandardSubjectBuilderCallback assertionCallback) Captures and returns the failure produced by the assertion in the provided callback, similar toassertThrows():static <S extends Subject, A>
AssertionErrorexpectFailureAbout(Subject.Factory<S, A> factory, ExpectFailure.SimpleSubjectBuilderCallback<S, A> assertionCallback) Captures and returns the failure produced by the assertion in the provided callback, similar toassertThrows():Legacy method that returns the failure captured bywhenTesting(), if one occurred.Legacy method that returns a subject builder that expects the chained assertion to fail, and makes the failure available viagetFailure().
-
Constructor Details
-
ExpectFailure
public ExpectFailure()Creates a new instance for use as a@Rule. See the class documentation for details, and consider using the lambda version instead.
-
-
Method Details
-
whenTesting
Legacy method that returns a subject builder that expects the chained assertion to fail, and makes the failure available viagetFailure().An instance of
ExpectFailuresupports only onewhenTestingcall per test method. The staticexpectFailure(com.google.common.truth.ExpectFailure.StandardSubjectBuilderCallback)method, by contrast, does not have this limitation. -
getFailure
Legacy method that returns the failure captured bywhenTesting(), if one occurred. -
expectFailure
@CanIgnoreReturnValue public static AssertionError expectFailure(ExpectFailure.StandardSubjectBuilderCallback assertionCallback) Captures and returns the failure produced by the assertion in the provided callback, similar toassertThrows():AssertionError e = expectFailure(whenTesting -> whenTesting.that(4).isNotEqualTo(4)); -
expectFailureAbout
@CanIgnoreReturnValue public static <S extends Subject, A> AssertionError expectFailureAbout(Subject.Factory<S, A> factory, ExpectFailure.SimpleSubjectBuilderCallback<S, A> assertionCallback) Captures and returns the failure produced by the assertion in the provided callback, similar toassertThrows():AssertionError e = expectFailureAbout(myTypes(), whenTesting -> whenTesting.that(myType).hasProperty()); -
assertThat
Creates a subject for asserting about the givenAssertionError, usually one produced by Truth. -
apply
@GwtIncompatible("org.junit.rules.TestRule") public Statement apply(Statement base, Description description)
-