Class Fact

  • All Implemented Interfaces:
    java.io.Serializable

    public final class Fact
    extends java.lang.Object
    implements java.io.Serializable
    A string key-value pair in a failure message, such as "expected: abc" or "but was: xyz."

    Most Truth users will never interact with this type. It appears in the Truth API only as a parameter to methods like Subject.failWithActual(Fact, Fact...), which are used only by custom Subject implementations.

    If you are writing a custom Subject, see our tips on writing failure messages.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) java.lang.String key  
      (package private) java.lang.String value  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Fact​(java.lang.String key, java.lang.String value)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static Fact fact​(java.lang.String key, java.lang.Object value)
      Creates a fact with the given key and value, which will be printed in a format like "key: value." The value is converted to a string by calling String.valueOf on it.
      private static java.lang.String indent​(java.lang.String value)  
      (package private) static java.lang.String makeMessage​(com.google.common.collect.ImmutableList<java.lang.String> messages, com.google.common.collect.ImmutableList<Fact> facts)
      Formats the given messages and facts into a string for use as the message of a test failure.
      static Fact simpleFact​(java.lang.String key)
      Creates a fact with no value, which will be printed in the format "key" (with no colon or value).
      java.lang.String toString()
      Returns a simple string representation for the fact.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • key

        final java.lang.String key
      • value

        final java.lang.String value
    • Constructor Detail

      • Fact

        private Fact​(java.lang.String key,
                     java.lang.String value)
    • Method Detail

      • fact

        public static Fact fact​(java.lang.String key,
                                java.lang.Object value)
        Creates a fact with the given key and value, which will be printed in a format like "key: value." The value is converted to a string by calling String.valueOf on it.
      • simpleFact

        public static Fact simpleFact​(java.lang.String key)
        Creates a fact with no value, which will be printed in the format "key" (with no colon or value).

        In most cases, prefer key-value facts, which give Truth more flexibility in how to format the fact for display. simpleFact is useful primarily for:

        • messages from no-arg assertions. For example, isNotEmpty() would generate the fact "expected not to be empty"
        • prose that is part of a larger message. For example, contains() sometimes displays facts like "expected to contain: ..." "but did not" "though it did contain: ..."
      • toString

        public java.lang.String toString()
        Returns a simple string representation for the fact. While this is used in the output of TruthFailureSubject, it's not used in normal failure messages, which automatically align facts horizontally and indent multiline values.
        Overrides:
        toString in class java.lang.Object
      • makeMessage

        static java.lang.String makeMessage​(com.google.common.collect.ImmutableList<java.lang.String> messages,
                                            com.google.common.collect.ImmutableList<Fact> facts)
        Formats the given messages and facts into a string for use as the message of a test failure. In particular, this method horizontally aligns the beginning of fact values.
      • indent

        private static java.lang.String indent​(java.lang.String value)