Class Truth
- java.lang.Object
-
- com.google.common.truth.Truth
-
public final class Truth extends Object
The primary entry point for Truth, a library for fluent test assertions.Compare these example JUnit assertions...
...to their Truth equivalents...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));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 customFailureStrategy.) For more information, visit those types' docs.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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 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 <ComparableT extends Comparable<?>>
ComparableSubject<ComparableT>assertThat(@Nullable ComparableT actual)Begins an assertion about aComparable.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 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 throwAssertionError.
-
assertWithMessage
public static StandardSubjectBuilder assertWithMessage(@Nullable String message)
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(String format, @Nullable 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%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
public static BigDecimalSubject assertThat(@Nullable BigDecimal actual)
Begins an assertion about aBigDecimal.
-
assertThat
public static Subject assertThat(@Nullable Object actual)
Begins an assertion about anObject.
-
assertThat
@GwtIncompatible("ClassSubject.java") public static ClassSubject assertThat(@Nullable Class<?> actual)
Begins an assertion about aClass.
-
assertThat
public static ThrowableSubject assertThat(@Nullable Throwable actual)
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
public static LongSubject assertThat(@Nullable Long actual)
Begins an assertion about aLong.
-
assertThat
public static DoubleSubject assertThat(@Nullable Double actual)
Begins an assertion about aDouble.
-
assertThat
public static FloatSubject assertThat(@Nullable Float actual)
Begins an assertion about aFloat.
-
assertThat
public static IntegerSubject assertThat(@Nullable Integer actual)
Begins an assertion about anInteger.
-
assertThat
public static BooleanSubject assertThat(@Nullable Boolean actual)
Begins an assertion about aBoolean.
-
assertThat
public static StringSubject assertThat(@Nullable String actual)
Begins an assertion about aString.
-
assertThat
public static IterableSubject assertThat(@Nullable Iterable<?> actual)
Begins an assertion about anIterable.
-
assertThat
public static <T extends @Nullable Object> ObjectArraySubject<T> assertThat(T @Nullable [] actual)
Begins an assertion about anObjectarray.
-
assertThat
public static PrimitiveBooleanArraySubject assertThat(boolean @Nullable [] actual)
Begins an assertion about abooleanarray.
-
assertThat
public static PrimitiveShortArraySubject assertThat(short @Nullable [] actual)
Begins an assertion about ashortarray.
-
assertThat
public static PrimitiveIntArraySubject assertThat(int @Nullable [] actual)
Begins an assertion about anintarray.
-
assertThat
public static PrimitiveLongArraySubject assertThat(long @Nullable [] actual)
Begins an assertion about alongarray.
-
assertThat
public static PrimitiveByteArraySubject assertThat(byte @Nullable [] actual)
Begins an assertion about abytearray.
-
assertThat
public static PrimitiveCharArraySubject assertThat(char @Nullable [] actual)
Begins an assertion about achararray.
-
assertThat
public static PrimitiveFloatArraySubject assertThat(float @Nullable [] actual)
Begins an assertion about afloatarray.
-
assertThat
public static PrimitiveDoubleArraySubject assertThat(double @Nullable [] actual)
Begins an assertion about adoublearray.
-
assertThat
public static GuavaOptionalSubject assertThat(@Nullable Optional<?> actual)
Begins an assertion about a GuavaOptional.
-
assertThat
public static MapSubject assertThat(@Nullable Map<?,?> actual)
Begins an assertion about aMap.
-
assertThat
public static MultimapSubject assertThat(@Nullable Multimap<?,?> actual)
Begins an assertion about aMultimap.
-
assertThat
public static MultisetSubject assertThat(@Nullable Multiset<?> actual)
Begins an assertion about aMultiset.
-
assertThat
public static TableSubject assertThat(@Nullable Table<?,?,?> actual)
Begins an assertion about aTable.
-
assertThat
public static OptionalSubject assertThat(@Nullable Optional<?> actual)
Begins an assertion about anOptional.- Since:
- 1.3.0 (present in
Truth8since before 1.0)
-
assertThat
public static OptionalIntSubject assertThat(@Nullable OptionalInt actual)
Begins an assertion about anOptionalInt.- Since:
- 1.3.0 (present in
Truth8since before 1.0)
-
assertThat
public static OptionalLongSubject assertThat(@Nullable OptionalLong actual)
Begins an assertion about anOptionalLong.- Since:
- 1.4.0 (present in
Truth8since before 1.0)
-
assertThat
public static OptionalDoubleSubject assertThat(@Nullable OptionalDouble actual)
Begins an assertion about anOptionalDouble.- Since:
- 1.4.0 (present in
Truth8since before 1.0)
-
assertThat
public static StreamSubject assertThat(@Nullable Stream<?> actual)
Begins an assertion about aStream.- Since:
- 1.4.0 (present in
Truth8since before 1.0)
-
assertThat
public static IntStreamSubject assertThat(@Nullable IntStream actual)
Begins an assertion about anIntStream.- Since:
- 1.4.0 (present in
Truth8since before 1.0)
-
assertThat
public static LongStreamSubject assertThat(@Nullable LongStream actual)
Begins an assertion about aLongStream.- Since:
- 1.4.0 (present in
Truth8since before 1.0)
-
assertThat
@GwtIncompatible @J2ObjCIncompatible public static PathSubject assertThat(@Nullable Path actual)
Begins an assertion about aPath.- Since:
- 1.4.0 (present in
Truth8since before 1.0)
-
-