Class Truth
- java.lang.Object
-
- com.google.common.truth.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...
...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 ([email protected])
-
-
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)
static PrimitiveByteArraySubject
assertThat(byte @Nullable [] actual)
static PrimitiveCharArraySubject
assertThat(char @Nullable [] actual)
static PrimitiveDoubleArraySubject
assertThat(double @Nullable [] actual)
static PrimitiveFloatArraySubject
assertThat(float @Nullable [] actual)
static PrimitiveIntArraySubject
assertThat(int @Nullable [] actual)
static PrimitiveLongArraySubject
assertThat(long @Nullable [] actual)
static PrimitiveShortArraySubject
assertThat(short @Nullable [] actual)
static GuavaOptionalSubject
assertThat(@Nullable Optional<?> actual)
static MultimapSubject
assertThat(@Nullable Multimap<?,?> actual)
static MultisetSubject
assertThat(@Nullable Multiset<?> actual)
static TableSubject
assertThat(@Nullable Table<?,?,?> actual)
static <ComparableT extends java.lang.Comparable<?>>
ComparableSubject<ComparableT>assertThat(@Nullable ComparableT actual)
static BooleanSubject
assertThat(@Nullable java.lang.Boolean actual)
static ClassSubject
assertThat(@Nullable java.lang.Class<?> actual)
static DoubleSubject
assertThat(@Nullable java.lang.Double actual)
static FloatSubject
assertThat(@Nullable java.lang.Float actual)
static IntegerSubject
assertThat(@Nullable java.lang.Integer actual)
static IterableSubject
assertThat(@Nullable java.lang.Iterable<?> actual)
static LongSubject
assertThat(@Nullable java.lang.Long actual)
static Subject
assertThat(@Nullable java.lang.Object actual)
static StringSubject
assertThat(@Nullable java.lang.String actual)
static ThrowableSubject
assertThat(@Nullable java.lang.Throwable actual)
static BigDecimalSubject
assertThat(@Nullable java.math.BigDecimal actual)
static MapSubject
assertThat(@Nullable java.util.Map<?,?> actual)
static <T> ObjectArraySubject<T>
assertThat(@Nullable T @Nullable [] actual)
static StandardSubjectBuilder
assertWithMessage(@Nullable java.lang.String messageToPrepend)
Begins an assertion that, if it fails, will prepend the given message to the failure message.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.
-
-
-
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 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 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 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
@GwtIncompatible("ClassSubject.java") public static ClassSubject assertThat(@Nullable java.lang.Class<?> actual)
-
assertThat
public static ThrowableSubject assertThat(@Nullable java.lang.Throwable actual)
-
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> ObjectArraySubject<T> assertThat(@Nullable T @Nullable [] actual)
-
assertThat
public static PrimitiveBooleanArraySubject assertThat(boolean @Nullable [] actual)
-
assertThat
public static PrimitiveShortArraySubject assertThat(short @Nullable [] actual)
-
assertThat
public static PrimitiveIntArraySubject assertThat(int @Nullable [] actual)
-
assertThat
public static PrimitiveLongArraySubject assertThat(long @Nullable [] actual)
-
assertThat
public static PrimitiveByteArraySubject assertThat(byte @Nullable [] actual)
-
assertThat
public static PrimitiveCharArraySubject assertThat(char @Nullable [] actual)
-
assertThat
public static PrimitiveFloatArraySubject assertThat(float @Nullable [] actual)
-
assertThat
public static PrimitiveDoubleArraySubject assertThat(double @Nullable [] actual)
-
assertThat
public static GuavaOptionalSubject assertThat(@Nullable Optional<?> actual)
-
assertThat
public static MapSubject assertThat(@Nullable java.util.Map<?,?> actual)
-
assertThat
public static MultimapSubject assertThat(@Nullable Multimap<?,?> actual)
-
assertThat
public static MultisetSubject assertThat(@Nullable Multiset<?> actual)
-
assertThat
public static TableSubject assertThat(@Nullable Table<?,?,?> actual)
-
-