Class TruthFailureSubject
java.lang.Object
com.google.common.truth.Subject
com.google.common.truth.ThrowableSubject
com.google.common.truth.TruthFailureSubject
A subject for
AssertionError objects thrown by Truth. TruthFailureSubject
contains methods for asserting about the individual "facts" of those failures. This allows tests
to avoid asserting about the same fact more often than necessary, including avoiding asserting
about facts that are set by other subjects that the main subject delegates to. This keeps tests
shorter and less fragile.
To create an instance, call ExpectFailure.assertThat(AssertionError). Or, if you're using a custom
message or failure strategy, pass truthFailures() to your about(...) call.
This class accepts any AssertionError value, but it will throw an exception if a
caller tries to access the facts of an error that wasn't produced by Truth.
-
Nested Class Summary
Nested classes/interfaces inherited from class Subject
Subject.Factory<SubjectT,ActualT> -
Method Summary
Modifier and TypeMethodDescriptionfactKeys()Returns a subject for the list of fact keys.Returns a subject for the value with the given name.Returns a subject for the value of theindex-th instance of the fact with the given name.Factory for creatingTruthFailureSubjectinstances.Methods inherited from class ThrowableSubject
hasCauseThat, hasMessageThatMethods inherited from class Subject
actualCustomStringRepresentation, check, equals, failWithActual, failWithActual, failWithoutActual, hashCode, ignoreCheck, isAnyOf, isEqualTo, isIn, isInstanceOf, isNoneOf, isNotEqualTo, isNotIn, isNotInstanceOf, isNotNull, isNotSameInstanceAs, isNull, isSameInstanceAs, toStringModifier and TypeMethodDescriptionprotected StringReturns a string representation of the actual value for inclusion in failure messages.protected final StandardSubjectBuilderReturns a builder for creating a derived subject.final booleanDeprecated.protected final voidfailWithActual(Fact first, Fact... rest) Fails, reporting a message with the given facts, followed by an automatically added fact of the form: but was: actual value.protected final voidfailWithActual(String key, @Nullable Object value) Fails, reporting a message with two "facts": key: value but was: actual value.protected final voidfailWithoutActual(Fact first, Fact... rest) Fails, reporting a message with the given facts, without automatically adding the actual value.final inthashCode()Deprecated.Object.hashCode()is not supported on Truth subjects.protected final StandardSubjectBuilderBegins a new call chain that ignores any failures.voidChecks that the value under test is equal to any of the given elements.voidChecks that the value under test is equal to the given object.voidChecks that the value under test is equal to any element in the given iterable.voidisInstanceOf(@Nullable Class<?> clazz) Checks that the value under test is an instance of the given class.voidChecks that the value under test is not equal to any of the given elements.voidisNotEqualTo(@Nullable Object other) Checks that the value under test is not equal to the given object.voidChecks that the value under test is not equal to any element in the given iterable.voidisNotInstanceOf(@Nullable Class<?> clazz) Checks that the value under test is not an instance of the given class.voidChecks that the value under test is not null.final voidisNotSameInstanceAs(@Nullable Object other) Checks that the value under test is not the same instance as the given object.voidisNull()Checks that the value under test is null.final voidisSameInstanceAs(@Nullable Object expected) Checks that the value under test is the same instance as the given object.toString()Deprecated.Object.toString()is not supported on Truth subjects.
-
Method Details
-
truthFailures
Factory for creatingTruthFailureSubjectinstances. Most users will just useExpectFailure.assertThat(AssertionError). -
factKeys
Returns a subject for the list of fact keys. -
factValue
Returns a subject for the value with the given name.The value is always a string, the
String.valueOfrepresentation of the value passed toFact.fact(String, Object).The value is never null:
- In the case of facts that have no value,
factValuethrows an exception. To test for such facts, usefactKeys().contains(...)or a similar method. - In the case of facts that have a value that is rendered as "null" (such as those created
with
fact("key", null)),factValueconsiders them have a string value, the string "null."
If the failure under test contains more than one fact with the given key, this method will fail the test. To assert about such a failure, use the other overload of
factValue. - In the case of facts that have no value,
-
factValue
Returns a subject for the value of theindex-th instance of the fact with the given name. Most Truth failures do not contain multiple facts with the same key, so most tests should use the other overload offactValue.
-
Object.equals(Object)is not supported on Truth subjects.