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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
Truth.SimpleAssertionError
AnAssertionError
that (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 StandardSubjectBuilder
ASSERT
private static FailureStrategy
THROW_ASSERTION_ERROR
-
Constructor Summary
Constructors Modifier Constructor Description private
Truth()
-
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(com.google.common.base.Optional<?> actual)
static MultimapSubject
assertThat(com.google.common.collect.Multimap<?,?> actual)
static MultisetSubject
assertThat(com.google.common.collect.Multiset<?> actual)
static TableSubject
assertThat(com.google.common.collect.Table<?,?,?> actual)
static BooleanSubject
assertThat(java.lang.Boolean actual)
static ClassSubject
assertThat(java.lang.Class<?> actual)
static DoubleSubject
assertThat(java.lang.Double actual)
static FloatSubject
assertThat(java.lang.Float actual)
static IntegerSubject
assertThat(java.lang.Integer actual)
static IterableSubject
assertThat(java.lang.Iterable<?> actual)
static LongSubject
assertThat(java.lang.Long actual)
static Subject
assertThat(java.lang.Object actual)
static StringSubject
assertThat(java.lang.String actual)
static ThrowableSubject
assertThat(java.lang.Throwable actual)
static BigDecimalSubject
assertThat(java.math.BigDecimal actual)
static MapSubject
assertThat(java.util.Map<?,?> actual)
static <T extends java.lang.Comparable<?>>
ComparableSubject<T>assertThat(T actual)
static <T> ObjectArraySubject<T>
assertThat(T[] actual)
static StandardSubjectBuilder
assertWithMessage(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, 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%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 <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)
-
-