Class Truth


  • public final class Truth
    extends java.lang.Object
    The primary entry point for Truth, a library for fluent test assertions.

    Compare these example JUnit assertions...

    
     assertEquals(b, a);
     assertTrue(c);
     assertTrue(d.contains(a));
     assertTrue(d.contains(a) && d.contains(b));
     assertTrue(d.contains(a) || d.contains(b) || d.contains(c));
     
    ...to their Truth equivalents...
    
     assertThat(a).isEqualTo(b);
     assertThat(c).isTrue();
     assertThat(d).contains(a);
     assertThat(d).containsAtLeast(a, b);
     assertThat(d).containsAnyOf(a, b, c);
     

    Advantages of Truth:

    For more information about the methods in this class, see this FAQ entry.

    For people extending Truth

    The most common way to extend Truth is to write a custom Subject. (The other, much less common way is to write a custom FailureStrategy.) For more information, visit those types' docs.

    Author:
    David Saff, Christian Gruber (cgruber@israfil.net)
    • Method Detail

      • assert_

        public static StandardSubjectBuilder assert_()
        Begins a call chain with the fluent Truth API. If the check made by the chain fails, it will throw AssertionError.
      • assertWithMessage

        public static StandardSubjectBuilder assertWithMessage​(@Nullable java.lang.String messageToPrepend)
        Begins an assertion that, if it fails, will prepend the given message to the failure message.

        This method is a shortcut for assert_().withMessage(...).

        To set a message when using a custom subject, use assertWithMessage(...).about(...), as discussed in this FAQ entry.

      • assertWithMessage

        public static StandardSubjectBuilder assertWithMessage​(java.lang.String format,
                                                               @Nullable java.lang.Object... args)
        Begins an assertion that, if it fails, will prepend the given message to the failure message.

        Note: the arguments will be substituted into the format template using Strings.lenientFormat. Note this only supports the %s specifier.

        This method is a shortcut for assert_().withMessage(...).

        To set a message when using a custom subject, use assertWithMessage(...).about(...), as discussed in this FAQ entry.

        Throws:
        java.lang.IllegalArgumentException - if the number of placeholders in the format string does not equal the number of given arguments
      • assertAbout

        public static <S extends Subject,​T> SimpleSubjectBuilder<S,​T> assertAbout​(Subject.Factory<S,​T> factory)
        Given a factory for some Subject class, returns a builder whose that(actual) method creates instances of that class.
      • assertThat

        public static <ComparableT extends java.lang.Comparable<?>> ComparableSubject<ComparableT> assertThat​(@Nullable ComparableT actual)
      • assertThat

        public static BigDecimalSubject assertThat​(@Nullable java.math.BigDecimal actual)
      • assertThat

        public static Subject assertThat​(@Nullable java.lang.Object actual)
      • assertThat

        public static ThrowableSubject assertThat​(@Nullable java.lang.Throwable actual)
        Begins an assertion about a Throwable.

        Truth does not provide its own support for calling a method and automatically catching an expected exception, only for asserting on the exception after it has been caught. To catch the exception, we suggest assertThrows (JUnit), assertFailsWith (kotlin.test), or similar functionality from your testing library of choice.

         InvocationTargetException expected =
             assertThrows(InvocationTargetException.class, () -> method.invoke(null));
         assertThat(expected).hasCauseThat().isInstanceOf(IOException.class);
         
      • assertThat

        public static LongSubject assertThat​(@Nullable java.lang.Long actual)
      • assertThat

        public static DoubleSubject assertThat​(@Nullable java.lang.Double actual)
      • assertThat

        public static FloatSubject assertThat​(@Nullable java.lang.Float actual)
      • assertThat

        public static IntegerSubject assertThat​(@Nullable java.lang.Integer actual)
      • assertThat

        public static BooleanSubject assertThat​(@Nullable java.lang.Boolean actual)
      • assertThat

        public static StringSubject assertThat​(@Nullable java.lang.String actual)
      • assertThat

        public static IterableSubject assertThat​(@Nullable java.lang.Iterable<?> actual)
      • assertThat

        public static <T extends @Nullable java.lang.Object> ObjectArraySubject<T> assertThat​(T @Nullable [] actual)
      • assertThat

        public static MapSubject assertThat​(@Nullable java.util.Map<?,​?> actual)
      • assertThat

        public static TableSubject assertThat​(@Nullable Table<?,​?,​?> actual)
      • assertThat

        public static OptionalSubject assertThat​(@Nullable java.util.Optional<?> actual)
        Since:
        1.3.0 (present in Truth8 since before 1.0)
      • assertThat

        public static OptionalIntSubject assertThat​(@Nullable java.util.OptionalInt actual)
        Since:
        1.3.0 (present in Truth8 since before 1.0)
      • assertThat

        public static OptionalLongSubject assertThat​(@Nullable java.util.OptionalLong actual)
        Since:
        1.4.0 (present in Truth8 since before 1.0)
      • assertThat

        public static OptionalDoubleSubject assertThat​(@Nullable java.util.OptionalDouble actual)
        Since:
        1.4.0 (present in Truth8 since before 1.0)
      • assertThat

        public static StreamSubject assertThat​(@Nullable java.util.stream.Stream<?> actual)
        Since:
        1.4.0 (present in Truth8 since before 1.0)
      • assertThat

        public static IntStreamSubject assertThat​(@Nullable java.util.stream.IntStream actual)
        Since:
        1.4.0 (present in Truth8 since before 1.0)
      • assertThat

        public static LongStreamSubject assertThat​(@Nullable java.util.stream.LongStream actual)
        Since:
        1.4.0 (present in Truth8 since before 1.0)
      • assertThat

        @GwtIncompatible
        @J2ObjCIncompatible
        public static PathSubject assertThat​(@Nullable java.nio.file.Path actual)
        Since:
        1.4.0 (present in Truth8 since before 1.0)