Package com.google.common.truth
Class TruthFailureSubject
java.lang.Object
com.google.common.truth.Subject
com.google.common.truth.ThrowableSubject
com.google.common.truth.TruthFailureSubject
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(java.lang.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 com.google.common.truth.Subject
Subject.EqualityCheck, Subject.Factory<SubjectT extends Subject,
ActualT> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AssertionError
private static final Subject.Factory
<TruthFailureSubject, AssertionError> (package private) static final Fact
-
Constructor Summary
ConstructorsConstructorDescriptionTruthFailureSubject
(FailureMetadata metadata, AssertionError actual, String typeDescription) -
Method Summary
Modifier and TypeMethodDescriptionprivate StringSubject
doFactValue
(String key, Integer index) factKeys()
Returns a subject for the list of fact keys.private static com.google.common.collect.ImmutableList
<Fact> factsWithName
(ErrorWithFacts error, String key) 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.private static com.google.common.collect.ImmutableList
<String> getFactKeys
(ErrorWithFacts error) Factory for creatingTruthFailureSubject
instances.Methods inherited from class com.google.common.truth.ThrowableSubject
hasCauseThat, hasMessageThat
Methods inherited from class com.google.common.truth.Subject
actual, actualCustomStringRepresentation, actualCustomStringRepresentationForPackageMembersToCall, butWas, check, check, checkNoNeedToDisplayBothValues, equals, fail, fail, fail, failEqualityCheckForEqualsWithoutDescription, failWithActual, failWithActual, failWithActual, failWithActual, failWithBadResults, failWithCustomSubject, failWithoutActual, failWithoutActual, failWithoutActual, failWithoutActual, failWithoutSubject, hashCode, ignoreCheck, isAnyOf, isEqualTo, isIn, isInstanceOf, isNoneOf, isNotEqualTo, isNotIn, isNotInstanceOf, isNotNull, isNotSameInstanceAs, isNull, isSameInstanceAs, toString, typeDescription
-
Field Details
-
HOW_TO_TEST_KEYS_WITHOUT_VALUES
-
FACTORY
-
actual
-
-
Constructor Details
-
TruthFailureSubject
TruthFailureSubject(FailureMetadata metadata, AssertionError actual, String typeDescription)
-
-
Method Details
-
truthFailures
Factory for creatingTruthFailureSubject
instances. Most users will just useExpectFailure.assertThat(java.lang.AssertionError)
. -
factKeys
Returns a subject for the list of fact keys. -
getFactKeys
-
factValue
Returns a subject for the value with the given name.The value is always a string, the
String.valueOf
representation of the value passed toFact.fact(java.lang.String, java.lang.Object)
.The value is never null:
- In the case of facts that have no value,
factValue
throws 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)
),factValue
considers 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
. -
doFactValue
-
factsWithName
private static com.google.common.collect.ImmutableList<Fact> factsWithName(ErrorWithFacts error, String key)
-