Class Truth
- java.lang.Object
-
- com.google.common.truth.Truth
-
public final class Truth extends java.lang.ObjectThe 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classTruth.SimpleAssertionErrorAnAssertionErrorthat (a) always supports a cause, even under old versions of Android and (b) omits "java.lang.AssertionError:" from the beginning of its toString() representation.
-
Field Summary
Fields Modifier and Type Field Description private static StandardSubjectBuilderASSERTprivate static FailureStrategyTHROW_ASSERTION_ERROR
-
Constructor Summary
Constructors Modifier Constructor Description privateTruth()
-
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[] actual)static PrimitiveByteArraySubjectassertThat(byte[] actual)static PrimitiveCharArraySubjectassertThat(char[] actual)static PrimitiveDoubleArraySubjectassertThat(double[] actual)static PrimitiveFloatArraySubjectassertThat(float[] actual)static PrimitiveIntArraySubjectassertThat(int[] actual)static PrimitiveLongArraySubjectassertThat(long[] actual)static PrimitiveShortArraySubjectassertThat(short[] actual)static GuavaOptionalSubjectassertThat(com.google.common.base.Optional<?> actual)static MultimapSubjectassertThat(com.google.common.collect.Multimap<?,?> actual)static MultisetSubjectassertThat(com.google.common.collect.Multiset<?> actual)static TableSubjectassertThat(com.google.common.collect.Table<?,?,?> actual)static BooleanSubjectassertThat(java.lang.Boolean actual)static ClassSubjectassertThat(java.lang.Class<?> actual)static DoubleSubjectassertThat(java.lang.Double actual)static FloatSubjectassertThat(java.lang.Float actual)static IntegerSubjectassertThat(java.lang.Integer actual)static IterableSubjectassertThat(java.lang.Iterable<?> actual)static LongSubjectassertThat(java.lang.Long actual)static SubjectassertThat(java.lang.Object actual)static StringSubjectassertThat(java.lang.String actual)static ThrowableSubjectassertThat(java.lang.Throwable actual)static BigDecimalSubjectassertThat(java.math.BigDecimal actual)static MapSubjectassertThat(java.util.Map<?,?> actual)static <T extends java.lang.Comparable<?>>
ComparableSubject<T>assertThat(T actual)static <T> ObjectArraySubject<T>assertThat(T[] actual)static StandardSubjectBuilderassertWithMessage(java.lang.String messageToPrepend)Begins an assertion that, if it fails, will prepend the given message to the failure message.static StandardSubjectBuilderassertWithMessage(java.lang.String format, java.lang.Object... args)Begins an assertion that, if it fails, will prepend the given message to the failure message.
-
-
-
Field Detail
-
THROW_ASSERTION_ERROR
private static final FailureStrategy THROW_ASSERTION_ERROR
-
ASSERT
private static final StandardSubjectBuilder ASSERT
-
-
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(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, 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%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:
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 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 <T extends java.lang.Comparable<?>> ComparableSubject<T> assertThat(T actual)
-
assertThat
public static BigDecimalSubject assertThat(java.math.BigDecimal actual)
-
assertThat
public static Subject assertThat(java.lang.Object actual)
-
assertThat
public static ClassSubject assertThat(java.lang.Class<?> actual)
-
assertThat
public static ThrowableSubject assertThat(java.lang.Throwable actual)
-
assertThat
public static LongSubject assertThat(java.lang.Long actual)
-
assertThat
public static DoubleSubject assertThat(java.lang.Double actual)
-
assertThat
public static FloatSubject assertThat(java.lang.Float actual)
-
assertThat
public static IntegerSubject assertThat(java.lang.Integer actual)
-
assertThat
public static BooleanSubject assertThat(java.lang.Boolean actual)
-
assertThat
public static StringSubject assertThat(java.lang.String actual)
-
assertThat
public static IterableSubject assertThat(java.lang.Iterable<?> actual)
-
assertThat
public static <T> ObjectArraySubject<T> assertThat(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(com.google.common.base.Optional<?> actual)
-
assertThat
public static MapSubject assertThat(java.util.Map<?,?> actual)
-
assertThat
public static MultimapSubject assertThat(com.google.common.collect.Multimap<?,?> actual)
-
assertThat
public static MultisetSubject assertThat(com.google.common.collect.Multiset<?> actual)
-
assertThat
public static TableSubject assertThat(com.google.common.collect.Table<?,?,?> actual)
-
-