Class Truth
Compare these example JUnit assertions...
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));
...to their Truth equivalents...
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 custom FailureStrategy
.) For more information, visit those
types' docs.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
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
FieldsModifier and TypeFieldDescriptionprivate static final StandardSubjectBuilder
private static final FailureStrategy
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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
(Boolean actual) static ClassSubject
assertThat
(Class<?> actual) static DoubleSubject
assertThat
(Double actual) static FloatSubject
assertThat
(Float actual) static IntegerSubject
assertThat
(Integer actual) static IterableSubject
assertThat
(Iterable<?> actual) static LongSubject
assertThat
(Long actual) static Subject
assertThat
(Object actual) static StringSubject
assertThat
(String actual) static ThrowableSubject
assertThat
(Throwable actual) static BigDecimalSubject
assertThat
(BigDecimal actual) static MapSubject
assertThat
(Map<?, ?> actual) static <T extends Comparable<?>>
ComparableSubject<T>assertThat
(T actual) static <T> ObjectArraySubject<T>
assertThat
(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.
-
Field Details
-
THROW_ASSERTION_ERROR
-
ASSERT
-
-
Constructor Details
-
Truth
private Truth()
-
-
Method Details
-
assert_
Begins a call chain with the fluent Truth API. If the check made by the chain fails, it will throwAssertionError
. -
assertWithMessage
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
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
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-
assertThat
-