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
-
public final class TruthFailureSubject extends ThrowableSubject
Subject forAssertionError
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, passtruthFailures()
to yourabout(...)
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.Factory<SubjectT extends Subject,ActualT>
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description IterableSubject
factKeys()
Returns a subject for the list of fact keys.StringSubject
factValue(String key)
Returns a subject for the value with the given name.StringSubject
factValue(String key, int index)
Returns a subject for the value of theindex
-th instance of the fact with the given name.static Subject.Factory<TruthFailureSubject,AssertionError>
truthFailures()
Factory for creatingTruthFailureSubject
instances.-
Methods inherited from class com.google.common.truth.ThrowableSubject
hasCauseThat, hasMessageThat
-
Methods inherited from class com.google.common.truth.Subject
actualCustomStringRepresentation, check, equals, failWithActual, failWithActual, failWithoutActual, hashCode, ignoreCheck, isAnyOf, isEqualTo, isIn, isInstanceOf, isNoneOf, isNotEqualTo, isNotIn, isNotInstanceOf, isNotNull, isNotSameInstanceAs, isNull, isSameInstanceAs, toString
-
-
-
-
Method Detail
-
truthFailures
public static Subject.Factory<TruthFailureSubject,AssertionError> truthFailures()
Factory for creatingTruthFailureSubject
instances. Most users will just useExpectFailure.assertThat(java.lang.AssertionError)
.
-
factKeys
public IterableSubject factKeys()
Returns a subject for the list of fact keys.
-
factValue
public StringSubject factValue(String key)
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
public StringSubject factValue(String key, int index)
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
.
-
-