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