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.- Author:
- David Saff, Christian Gruber (cgruber@israfil.net)
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StandardSubjectBuilder
assert_()
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 someSubject
class, returns a builder whosethat(actual)
method creates instances of that class.static PrimitiveBooleanArraySubject
assertThat(boolean @Nullable [] actual)
Begins an assertion about aboolean
array.static PrimitiveByteArraySubject
assertThat(byte @Nullable [] actual)
Begins an assertion about abyte
array.static PrimitiveCharArraySubject
assertThat(char @Nullable [] actual)
Begins an assertion about achar
array.static PrimitiveDoubleArraySubject
assertThat(double @Nullable [] actual)
Begins an assertion about adouble
array.static PrimitiveFloatArraySubject
assertThat(float @Nullable [] actual)
Begins an assertion about afloat
array.static PrimitiveIntArraySubject
assertThat(int @Nullable [] actual)
Begins an assertion about anint
array.static PrimitiveLongArraySubject
assertThat(long @Nullable [] actual)
Begins an assertion about along
array.static PrimitiveShortArraySubject
assertThat(short @Nullable [] actual)
Begins an assertion about ashort
array.static GuavaOptionalSubject
assertThat(@Nullable Optional<?> actual)
Begins an assertion about a GuavaOptional
.static MultimapSubject
assertThat(@Nullable Multimap<?,?> actual)
Begins an assertion about aMultimap
.static MultisetSubject
assertThat(@Nullable Multiset<?> actual)
Begins an assertion about aMultiset
.static TableSubject
assertThat(@Nullable Table<?,?,?> actual)
Begins an assertion about aTable
.static <ComparableT extends Comparable<?>>
ComparableSubject<ComparableT>assertThat(@Nullable ComparableT actual)
Begins an assertion about aComparable
.static BooleanSubject
assertThat(@Nullable Boolean actual)
Begins an assertion about aBoolean
.static ClassSubject
assertThat(@Nullable Class<?> actual)
Begins an assertion about aClass
.static DoubleSubject
assertThat(@Nullable Double actual)
Begins an assertion about aDouble
.static FloatSubject
assertThat(@Nullable Float actual)
Begins an assertion about aFloat
.static IntegerSubject
assertThat(@Nullable Integer actual)
Begins an assertion about anInteger
.static IterableSubject
assertThat(@Nullable Iterable<?> actual)
Begins an assertion about anIterable
.static LongSubject
assertThat(@Nullable Long actual)
Begins an assertion about aLong
.static Subject
assertThat(@Nullable Object actual)
Begins an assertion about anObject
.static StringSubject
assertThat(@Nullable String actual)
Begins an assertion about aString
.static ThrowableSubject
assertThat(@Nullable Throwable actual)
Begins an assertion about aThrowable
.static BigDecimalSubject
assertThat(@Nullable BigDecimal actual)
Begins an assertion about aBigDecimal
.static PathSubject
assertThat(@Nullable Path actual)
Begins an assertion about aPath
.static MapSubject
assertThat(@Nullable Map<?,?> actual)
Begins an assertion about aMap
.static OptionalSubject
assertThat(@Nullable Optional<?> actual)
Begins an assertion about anOptional
.static OptionalDoubleSubject
assertThat(@Nullable OptionalDouble actual)
Begins an assertion about anOptionalDouble
.static OptionalIntSubject
assertThat(@Nullable OptionalInt actual)
Begins an assertion about anOptionalInt
.static OptionalLongSubject
assertThat(@Nullable OptionalLong actual)
Begins an assertion about anOptionalLong
.static IntStreamSubject
assertThat(@Nullable IntStream actual)
Begins an assertion about anIntStream
.static LongStreamSubject
assertThat(@Nullable LongStream actual)
Begins an assertion about aLongStream
.static StreamSubject
assertThat(@Nullable Stream<?> actual)
Begins an assertion about aStream
.static <T extends @Nullable Object>
ObjectArraySubject<T>assertThat(T @Nullable [] actual)
Begins an assertion about anObject
array.static StandardSubjectBuilder
assertWithMessage(@Nullable String messageToPrepend)
Begins an assertion that, if it fails, will prepend the given message to the failure message.static StandardSubjectBuilder
assertWithMessage(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 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(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%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:
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 someSubject
class, 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.Factory
if 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 anObject
array.
-
assertThat
public static PrimitiveBooleanArraySubject assertThat(boolean @Nullable [] actual)
Begins an assertion about aboolean
array.
-
assertThat
public static PrimitiveShortArraySubject assertThat(short @Nullable [] actual)
Begins an assertion about ashort
array.
-
assertThat
public static PrimitiveIntArraySubject assertThat(int @Nullable [] actual)
Begins an assertion about anint
array.
-
assertThat
public static PrimitiveLongArraySubject assertThat(long @Nullable [] actual)
Begins an assertion about along
array.
-
assertThat
public static PrimitiveByteArraySubject assertThat(byte @Nullable [] actual)
Begins an assertion about abyte
array.
-
assertThat
public static PrimitiveCharArraySubject assertThat(char @Nullable [] actual)
Begins an assertion about achar
array.
-
assertThat
public static PrimitiveFloatArraySubject assertThat(float @Nullable [] actual)
Begins an assertion about afloat
array.
-
assertThat
public static PrimitiveDoubleArraySubject assertThat(double @Nullable [] actual)
Begins an assertion about adouble
array.
-
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
Truth8
since before 1.0)
-
assertThat
public static OptionalIntSubject assertThat(@Nullable OptionalInt actual)
Begins an assertion about anOptionalInt
.- Since:
- 1.3.0 (present in
Truth8
since before 1.0)
-
assertThat
public static OptionalLongSubject assertThat(@Nullable OptionalLong actual)
Begins an assertion about anOptionalLong
.- Since:
- 1.4.0 (present in
Truth8
since before 1.0)
-
assertThat
public static OptionalDoubleSubject assertThat(@Nullable OptionalDouble actual)
Begins an assertion about anOptionalDouble
.- Since:
- 1.4.0 (present in
Truth8
since before 1.0)
-
assertThat
public static StreamSubject assertThat(@Nullable Stream<?> actual)
Begins an assertion about aStream
.- Since:
- 1.4.0 (present in
Truth8
since before 1.0)
-
assertThat
public static IntStreamSubject assertThat(@Nullable IntStream actual)
Begins an assertion about anIntStream
.- Since:
- 1.4.0 (present in
Truth8
since before 1.0)
-
assertThat
public static LongStreamSubject assertThat(@Nullable LongStream actual)
Begins an assertion about aLongStream
.- Since:
- 1.4.0 (present in
Truth8
since before 1.0)
-
assertThat
@GwtIncompatible @J2ObjCIncompatible public static PathSubject assertThat(@Nullable Path actual)
Begins an assertion about aPath
.- Since:
- 1.4.0 (present in
Truth8
since before 1.0)
-
-