Class Truth
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:
- aligns all the "actual" values on the left
- produces more detailed failure messages
- provides richer operations (like
IterableSubject.containsExactly(java.lang.Object...))
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.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StandardSubjectBuilderassert_()Begins a call chain with the fluent Truth API.static <CustomSubjectBuilderT extends CustomSubjectBuilder>
CustomSubjectBuilderTassertAbout(CustomSubjectBuilder.Factory<CustomSubjectBuilderT> factory) A generic, advanced method of extension of Truth to new types, which is documented onCustomSubjectBuilder.static <S extends Subject, T>
SimpleSubjectBuilder<S, T> assertAbout(Subject.Factory<S, T> factory) Given a factory for someSubjectclass, returns a builder whosethat(actual)method creates instances of that class.static PrimitiveBooleanArraySubjectassertThat(boolean @Nullable [] actual) Begins an assertion about abooleanarray.static PrimitiveByteArraySubjectassertThat(byte @Nullable [] actual) Begins an assertion about abytearray.static PrimitiveCharArraySubjectassertThat(char @Nullable [] actual) Begins an assertion about achararray.static PrimitiveDoubleArraySubjectassertThat(double @Nullable [] actual) Begins an assertion about adoublearray.static PrimitiveFloatArraySubjectassertThat(float @Nullable [] actual) Begins an assertion about afloatarray.static PrimitiveIntArraySubjectassertThat(int @Nullable [] actual) Begins an assertion about anintarray.static PrimitiveLongArraySubjectassertThat(long @Nullable [] actual) Begins an assertion about alongarray.static PrimitiveShortArraySubjectassertThat(short @Nullable [] actual) Begins an assertion about ashortarray.static <ComparableT extends Comparable<?>>
ComparableSubject<ComparableT> assertThat(@Nullable ComparableT actual) Begins an assertion about aComparable.static GuavaOptionalSubjectassertThat(@Nullable Optional<?> actual) Begins an assertion about a GuavaOptional.static MultimapSubjectassertThat(@Nullable Multimap<?, ?> actual) Begins an assertion about aMultimap.static MultisetSubjectassertThat(@Nullable Multiset<?> actual) Begins an assertion about aMultiset.static TableSubjectassertThat(@Nullable Table<?, ?, ?> actual) Begins an assertion about aTable.static BooleanSubjectassertThat(@Nullable Boolean actual) Begins an assertion about aBoolean.static ClassSubjectassertThat(@Nullable Class<?> actual) Begins an assertion about aClass.static DoubleSubjectassertThat(@Nullable Double actual) Begins an assertion about aDouble.static FloatSubjectassertThat(@Nullable Float actual) Begins an assertion about aFloat.static IntegerSubjectassertThat(@Nullable Integer actual) Begins an assertion about anInteger.static IterableSubjectassertThat(@Nullable Iterable<?> actual) Begins an assertion about anIterable.static LongSubjectassertThat(@Nullable Long actual) Begins an assertion about aLong.static SubjectassertThat(@Nullable Object actual) Begins an assertion about anObject.static StringSubjectassertThat(@Nullable String actual) Begins an assertion about aString.static ThrowableSubjectassertThat(@Nullable Throwable actual) Begins an assertion about aThrowable.static BigDecimalSubjectassertThat(@Nullable BigDecimal actual) Begins an assertion about aBigDecimal.static PathSubjectassertThat(@Nullable Path actual) Begins an assertion about aPath.static MapSubjectassertThat(@Nullable Map<?, ?> actual) Begins an assertion about aMap.static OptionalSubjectassertThat(@Nullable Optional<?> actual) Begins an assertion about anOptional.static OptionalDoubleSubjectassertThat(@Nullable OptionalDouble actual) Begins an assertion about anOptionalDouble.static OptionalIntSubjectassertThat(@Nullable OptionalInt actual) Begins an assertion about anOptionalInt.static OptionalLongSubjectassertThat(@Nullable OptionalLong actual) Begins an assertion about anOptionalLong.static IntStreamSubjectassertThat(@Nullable IntStream actual) Begins an assertion about anIntStream.static LongStreamSubjectassertThat(@Nullable LongStream actual) Begins an assertion about aLongStream.static StreamSubjectassertThat(@Nullable Stream<?> actual) Begins an assertion about aStream.static <T extends @Nullable Object>
ObjectArraySubject<T> assertThat(T @Nullable [] actual) Begins an assertion about anObjectarray.static StandardSubjectBuilderassertWithMessage(@Nullable String message) Begins an assertion that, if it fails, will prepend the given message to the failure message.static StandardSubjectBuilderassertWithMessage(String format, @Nullable Object... args) Begins an assertion that, if it fails, will prepend the given message to the failure message.
-
Method Details
-
assert_
Begins a call chain with the fluent Truth API. If the check made by the chain fails, it will throwAssertionError. -
assertWithMessage
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
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%sspecifier.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:
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 someSubjectclass, returns a builder whosethat(actual)method creates instances of that class. -
assertAbout
public static <CustomSubjectBuilderT extends CustomSubjectBuilder> CustomSubjectBuilderT assertAbout(CustomSubjectBuilder.Factory<CustomSubjectBuilderT> factory) A generic, advanced method of extension of Truth to new types, which is documented onCustomSubjectBuilder. Extension creators should preferSubject.Factoryif possible. -
assertThat
public static <ComparableT extends Comparable<?>> ComparableSubject<ComparableT> assertThat(@Nullable ComparableT actual) Begins an assertion about aComparable. -
assertThat
Begins an assertion about aBigDecimal. -
assertThat
-
assertThat
@GwtIncompatible("ClassSubject.java") public static ClassSubject assertThat(@Nullable Class<?> actual) Begins an assertion about aClass. -
assertThat
Begins an assertion about aThrowable.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
Begins an assertion about aLong. -
assertThat
Begins an assertion about aDouble. -
assertThat
Begins an assertion about aFloat. -
assertThat
Begins an assertion about anInteger. -
assertThat
Begins an assertion about aBoolean. -
assertThat
Begins an assertion about aString. -
assertThat
Begins an assertion about anIterable. -
assertThat
Begins an assertion about anObjectarray. -
assertThat
Begins an assertion about abooleanarray. -
assertThat
Begins an assertion about ashortarray. -
assertThat
Begins an assertion about anintarray. -
assertThat
Begins an assertion about alongarray. -
assertThat
Begins an assertion about abytearray. -
assertThat
Begins an assertion about achararray. -
assertThat
Begins an assertion about afloatarray. -
assertThat
Begins an assertion about adoublearray. -
assertThat
Begins an assertion about a GuavaOptional. -
assertThat
Begins an assertion about aMap. -
assertThat
Begins an assertion about aMultimap. -
assertThat
Begins an assertion about aMultiset. -
assertThat
Begins an assertion about aTable. -
assertThat
-
assertThat
Begins an assertion about anOptionalInt.- Since:
- 1.3.0 (present in
Truth8since before 1.0)
-
assertThat
Begins an assertion about anOptionalLong.- Since:
- 1.4.0 (present in
Truth8since before 1.0)
-
assertThat
Begins an assertion about anOptionalDouble.- Since:
- 1.4.0 (present in
Truth8since before 1.0)
-
assertThat
-
assertThat
-
assertThat
Begins an assertion about aLongStream.- Since:
- 1.4.0 (present in
Truth8since before 1.0)
-
assertThat
-