public final class Truth extends Object
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).containsAllOf(a, b);
assertThat(d).containsAnyOf(a, b, c);
Advantages of Truth:
IterableSubject.containsExactly(java.lang.Object...)
)
TODO(cpovirk): Link to a doc about the full assertion chain.
TODO(cpovirk): Link to a doc about custom subjects.
TODO(cpovirk): Also talk about FailureStrategy
.
Modifier and Type | Field and Description |
---|---|
static FailureStrategy |
THROW_ASSERTION_ERROR
Deprecated.
If you are passing this to a
Subject constructor, instead call assertAbout(SubjectFactory) .that(...) , passing its associated SubjectFactory (which you might need to create). If you are calling fail on this
object directly, instead use assert_() .fail() , or throw new
AssertionError() directly. |
Modifier and Type | Method and Description |
---|---|
static StandardSubjectBuilder |
assert_()
Begins a call chain with the fluent Truth API.
|
static <CustomSubjectBuilderT extends CustomSubjectBuilder> |
assertAbout(CustomSubjectBuilder.Factory<CustomSubjectBuilderT> factory)
A generic, advanced method of extension of Truth to new types, which is documented on
CustomSubjectBuilder . |
static <CustomSubjectBuilderT extends CustomSubjectBuilder> |
assertAbout(CustomSubjectBuilderFactory<CustomSubjectBuilderT> factory)
Deprecated.
When you switch from
CustomSubjectBuilderFactory to CustomSubjectBuilder.Factory , you'll switch from this overload to the CustomSubjectBuilder.Factory overload. |
static <S extends Subject<S,T>,T> |
assertAbout(Subject.Factory<S,T> factory)
Given a factory for some
Subject class, returns a builder whose that(actual)
method creates instances of that class. |
static <S extends Subject<S,T>,T> |
assertAbout(SubjectFactory<S,T> factory)
Deprecated.
When you switch from
SubjectFactory to Subject.Factory , you'll
switch from this overload to the Subject.Factory overload. |
static AtomicLongMapSubject |
assertThat(com.google.common.util.concurrent.AtomicLongMap<?> actual) |
static BigDecimalSubject |
assertThat(BigDecimal actual) |
static BooleanSubject |
assertThat(Boolean actual) |
static PrimitiveBooleanArraySubject |
assertThat(boolean[] actual) |
static PrimitiveByteArraySubject |
assertThat(byte[] actual) |
static PrimitiveCharArraySubject |
assertThat(char[] actual) |
static ClassSubject |
assertThat(Class<?> actual) |
static DoubleSubject |
assertThat(Double actual) |
static PrimitiveDoubleArraySubject |
assertThat(double[] actual) |
static FloatSubject |
assertThat(Float actual) |
static PrimitiveFloatArraySubject |
assertThat(float[] actual) |
static PrimitiveIntArraySubject |
assertThat(int[] actual) |
static IntegerSubject |
assertThat(Integer actual) |
static IterableSubject |
assertThat(Iterable<?> actual) |
static ListMultimapSubject |
assertThat(com.google.common.collect.ListMultimap<?,?> actual) |
static LongSubject |
assertThat(Long actual) |
static PrimitiveLongArraySubject |
assertThat(long[] actual) |
static MapSubject |
assertThat(Map<?,?> actual) |
static MultimapSubject |
assertThat(com.google.common.collect.Multimap<?,?> actual) |
static MultisetSubject |
assertThat(com.google.common.collect.Multiset<?> actual) |
static Subject<DefaultSubject,Object> |
assertThat(Object actual) |
static GuavaOptionalSubject |
assertThat(com.google.common.base.Optional<?> actual) |
static SetMultimapSubject |
assertThat(com.google.common.collect.SetMultimap<?,?> actual) |
static PrimitiveShortArraySubject |
assertThat(short[] actual) |
static SortedMapSubject |
assertThat(SortedMap<?,?> actual) |
static SortedSetSubject |
assertThat(SortedSet<?> actual) |
static StringSubject |
assertThat(String actual) |
static <T extends Comparable<?>> |
assertThat(T actual) |
static <T> ObjectArraySubject<T> |
assertThat(T[] actual) |
static TableSubject |
assertThat(com.google.common.collect.Table<?,?,?> actual) |
static ThrowableSubject |
assertThat(Throwable actual) |
static StandardSubjectBuilder |
assertWithMessage(String messageToPrepend)
Returns a
StandardSubjectBuilder that will prepend the given message to the failure
message in the event of a test failure. |
static StandardSubjectBuilder |
assertWithMessage(String format,
Object... args)
Returns a
StandardSubjectBuilder that will prepend the formatted message using the
specified arguments to the failure message in the event of a test failure. |
@Deprecated public static final FailureStrategy THROW_ASSERTION_ERROR
Subject
constructor, instead call assertAbout(SubjectFactory)
.that(...)
, passing its associated SubjectFactory
(which you might need to create). If you are calling fail
on this
object directly, instead use assert_()
.fail()
, or throw new
AssertionError()
directly.public static StandardSubjectBuilder assert_()
AssertionError
.public static StandardSubjectBuilder assertWithMessage(String messageToPrepend)
StandardSubjectBuilder
that will prepend the given message to the failure
message in the event of a test failure.public static StandardSubjectBuilder assertWithMessage(String format, Object... args)
StandardSubjectBuilder
that will prepend the formatted message using the
specified arguments to the failure message in the event of a test failure.
Note: The failure message template string only supports the "%s"
specifier,
not the full range of Formatter
specifiers.
IllegalArgumentException
- if the number of placeholders in the format string does not
equal the number of given arguments@Deprecated public static <S extends Subject<S,T>,T> SimpleSubjectBuilder<S,T> assertAbout(SubjectFactory<S,T> factory)
SubjectFactory
to Subject.Factory
, you'll
switch from this overload to the Subject.Factory
overload.com.google.common.truth.delegation.DelegationTest
.factory
- a SubjectFactorypublic static <S extends Subject<S,T>,T> SimpleSubjectBuilder<S,T> assertAbout(Subject.Factory<S,T> factory)
Subject
class, returns a builder whose that(actual)
method creates instances of that class.@Deprecated public static <CustomSubjectBuilderT extends CustomSubjectBuilder> CustomSubjectBuilderT assertAbout(CustomSubjectBuilderFactory<CustomSubjectBuilderT> factory)
CustomSubjectBuilderFactory
to CustomSubjectBuilder.Factory
, you'll switch from this overload to the CustomSubjectBuilder.Factory
overload.CustomSubjectBuilder
. Extension creators should prefer Subject.Factory
if possible.public static <CustomSubjectBuilderT extends CustomSubjectBuilder> CustomSubjectBuilderT assertAbout(CustomSubjectBuilder.Factory<CustomSubjectBuilderT> factory)
CustomSubjectBuilder
. Extension creators should prefer Subject.Factory
if possible.public static <T extends Comparable<?>> ComparableSubject<?,T> assertThat(@Nullable T actual)
public static BigDecimalSubject assertThat(@Nullable BigDecimal actual)
public static Subject<DefaultSubject,Object> assertThat(@Nullable Object actual)
@GwtIncompatible(value="ClassSubject.java") public static ClassSubject assertThat(@Nullable Class<?> actual)
public static ThrowableSubject assertThat(@Nullable Throwable actual)
public static LongSubject assertThat(@Nullable Long actual)
public static DoubleSubject assertThat(@Nullable Double actual)
public static FloatSubject assertThat(@Nullable Float actual)
public static IntegerSubject assertThat(@Nullable Integer actual)
public static BooleanSubject assertThat(@Nullable Boolean actual)
public static StringSubject assertThat(@Nullable String actual)
public static IterableSubject assertThat(@Nullable Iterable<?> actual)
public static SortedSetSubject assertThat(@Nullable SortedSet<?> actual)
public static <T> ObjectArraySubject<T> assertThat(@Nullable T[] actual)
public static PrimitiveBooleanArraySubject assertThat(@Nullable boolean[] actual)
public static PrimitiveShortArraySubject assertThat(@Nullable short[] actual)
public static PrimitiveIntArraySubject assertThat(@Nullable int[] actual)
public static PrimitiveLongArraySubject assertThat(@Nullable long[] actual)
public static PrimitiveByteArraySubject assertThat(@Nullable byte[] actual)
public static PrimitiveCharArraySubject assertThat(@Nullable char[] actual)
public static PrimitiveFloatArraySubject assertThat(@Nullable float[] actual)
public static PrimitiveDoubleArraySubject assertThat(@Nullable double[] actual)
public static GuavaOptionalSubject assertThat(@Nullable com.google.common.base.Optional<?> actual)
public static MapSubject assertThat(@Nullable Map<?,?> actual)
public static SortedMapSubject assertThat(@Nullable SortedMap<?,?> actual)
public static MultimapSubject assertThat(@Nullable com.google.common.collect.Multimap<?,?> actual)
public static ListMultimapSubject assertThat(@Nullable com.google.common.collect.ListMultimap<?,?> actual)
public static SetMultimapSubject assertThat(@Nullable com.google.common.collect.SetMultimap<?,?> actual)
public static MultisetSubject assertThat(@Nullable com.google.common.collect.Multiset<?> actual)
public static TableSubject assertThat(@Nullable com.google.common.collect.Table<?,?,?> actual)
public static AtomicLongMapSubject assertThat(@Nullable com.google.common.util.concurrent.AtomicLongMap<?> actual)
Copyright © 2017. All rights reserved.