Class TruthFailureSubject


  • public final class TruthFailureSubject
    extends ThrowableSubject
    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.

    • Method Detail

      • factKeys

        public IterableSubject factKeys()
        Returns a subject for the list of fact keys.
      • factValue

        public StringSubject factValue​(java.lang.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 to Fact.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, use factKeys() .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.

      • factValue

        public StringSubject factValue​(java.lang.String key,
                                       int index)
        Returns a subject for the value of the index-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 of factValue.