public final class Truth extends Object
Truth is the simplest entry point class. A developer can statically import the assertThat() method to get easy access to the library's capabilities. Then, instead of writing:
Assert.assertEquals(a, b);
Assert.assertTrue(c);
Assert.assertTrue(d.contains(a));
Assert.assertTrue(d.contains(a) && d.contains(b));
Assert.assertTrue(d.contains(a) || d.contains(b) || d.contains(c));
one would write:
assertThat(a).isEqualTo(b);
assertThat(c).isTrue();
assertThat(d).contains(a);
assertThat(d).containsAllOf(a, b);
assertThat(d).containsAnyOf(a, b, c);
Tests should be easier to read, and flow more clearly.
Often, tests assert a relationship between a value produced by the test (the "actual" value) and some reference value (the "expected" value). It is strongly recommended that the actual value is made the subject of the assertion. For example:
assertThat(actual).isEqualTo(expected); // recommended
assertThat(expected).isEqualTo(actual); // not recommended
assertThat(actual).isIn(expectedPossibilities); // recommended
assertThat(expectedPossibilities).contains(actual); // not recommended
Modifier and Type | Field and Description |
---|---|
static FailureStrategy |
THROW_ASSERTION_ERROR |
Modifier and Type | Method and Description |
---|---|
static TestVerb |
assert_() |
static <V extends AbstractDelegatedVerb<V>> |
assertAbout(DelegatedVerbFactory<V> factory)
A generic, advanced method of extension of Truth to new types, which is documented on
DelegatedVerbFactory . |
static <S extends Subject<S,T>,T> |
assertAbout(SubjectFactory<S,T> factory)
The recommended method of extension of Truth to new types, which is documented in
com.google.common.truth.delegation.DelegationTest . |
static AtomicLongMapSubject |
assertThat(com.google.common.util.concurrent.AtomicLongMap<?> target) |
static BigDecimalSubject |
assertThat(BigDecimal target) |
static BooleanSubject |
assertThat(Boolean target) |
static PrimitiveBooleanArraySubject |
assertThat(boolean[] target) |
static PrimitiveByteArraySubject |
assertThat(byte[] target) |
static PrimitiveCharArraySubject |
assertThat(char[] target) |
static ClassSubject |
assertThat(Class<?> target) |
static DoubleSubject |
assertThat(Double target) |
static PrimitiveDoubleArraySubject |
assertThat(double[] target) |
static FloatSubject |
assertThat(Float target) |
static PrimitiveFloatArraySubject |
assertThat(float[] target) |
static PrimitiveIntArraySubject |
assertThat(int[] target) |
static IntegerSubject |
assertThat(Integer target) |
static IterableSubject |
assertThat(Iterable<?> target) |
static ListMultimapSubject |
assertThat(com.google.common.collect.ListMultimap<?,?> target) |
static LongSubject |
assertThat(Long target) |
static PrimitiveLongArraySubject |
assertThat(long[] target) |
static MapSubject |
assertThat(Map<?,?> target) |
static MultimapSubject |
assertThat(com.google.common.collect.Multimap<?,?> target) |
static MultisetSubject |
assertThat(com.google.common.collect.Multiset<?> target) |
static Subject<DefaultSubject,Object> |
assertThat(Object target) |
static GuavaOptionalSubject |
assertThat(com.google.common.base.Optional<?> target) |
static SetMultimapSubject |
assertThat(com.google.common.collect.SetMultimap<?,?> target) |
static PrimitiveShortArraySubject |
assertThat(short[] target) |
static SortedMapSubject |
assertThat(SortedMap<?,?> target) |
static SortedSetSubject |
assertThat(SortedSet<?> target) |
static StringSubject |
assertThat(String target) |
static <T extends Comparable<?>> |
assertThat(T target) |
static <T> ObjectArraySubject<T> |
assertThat(T[] target) |
static TableSubject |
assertThat(com.google.common.collect.Table<?,?,?> target) |
static ThrowableSubject |
assertThat(Throwable target) |
static TestVerb |
assertWithMessage(String messageToPrepend)
Returns a
TestVerb that will prepend the given message to the failure message in the
event of a test failure. |
static TestVerb |
assertWithMessage(String format,
Object... args)
Returns a
TestVerb that will prepend the formatted message using the specified
arguments to the failure message in the event of a test failure. |
public static final FailureStrategy THROW_ASSERTION_ERROR
public static TestVerb assert_()
public static TestVerb assertWithMessage(String messageToPrepend)
TestVerb
that will prepend the given message to the failure message in the
event of a test failure.public static TestVerb assertWithMessage(String format, Object... args)
TestVerb
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 argumentspublic static <S extends Subject<S,T>,T> AbstractVerb.DelegatedVerb<S,T> assertAbout(SubjectFactory<S,T> factory)
com.google.common.truth.delegation.DelegationTest
.factory
- a SubjectFactorypublic static <V extends AbstractDelegatedVerb<V>> V assertAbout(DelegatedVerbFactory<V> factory)
DelegatedVerbFactory
. Extension creators should prefer SubjectFactory
if possible.V
- the type of AbstractDelegatedVerb
to returnfactory
- a DelegatedVerbFactory<V>
implementation<V>
public static <T extends Comparable<?>> ComparableSubject<?,T> assertThat(@Nullable T target)
public static BigDecimalSubject assertThat(@Nullable BigDecimal target)
public static Subject<DefaultSubject,Object> assertThat(@Nullable Object target)
@GwtIncompatible(value="ClassSubject.java") public static ClassSubject assertThat(@Nullable Class<?> target)
public static ThrowableSubject assertThat(@Nullable Throwable target)
public static LongSubject assertThat(@Nullable Long target)
public static DoubleSubject assertThat(@Nullable Double target)
public static FloatSubject assertThat(@Nullable Float target)
public static IntegerSubject assertThat(@Nullable Integer target)
public static BooleanSubject assertThat(@Nullable Boolean target)
public static StringSubject assertThat(@Nullable String target)
public static IterableSubject assertThat(@Nullable Iterable<?> target)
public static SortedSetSubject assertThat(@Nullable SortedSet<?> target)
public static <T> ObjectArraySubject<T> assertThat(@Nullable T[] target)
public static PrimitiveBooleanArraySubject assertThat(@Nullable boolean[] target)
public static PrimitiveShortArraySubject assertThat(@Nullable short[] target)
public static PrimitiveIntArraySubject assertThat(@Nullable int[] target)
public static PrimitiveLongArraySubject assertThat(@Nullable long[] target)
public static PrimitiveByteArraySubject assertThat(@Nullable byte[] target)
public static PrimitiveCharArraySubject assertThat(@Nullable char[] target)
public static PrimitiveFloatArraySubject assertThat(@Nullable float[] target)
public static PrimitiveDoubleArraySubject assertThat(@Nullable double[] target)
public static GuavaOptionalSubject assertThat(@Nullable com.google.common.base.Optional<?> target)
public static MapSubject assertThat(@Nullable Map<?,?> target)
public static SortedMapSubject assertThat(@Nullable SortedMap<?,?> target)
public static MultimapSubject assertThat(@Nullable com.google.common.collect.Multimap<?,?> target)
public static ListMultimapSubject assertThat(@Nullable com.google.common.collect.ListMultimap<?,?> target)
public static SetMultimapSubject assertThat(@Nullable com.google.common.collect.SetMultimap<?,?> target)
public static MultisetSubject assertThat(@Nullable com.google.common.collect.Multiset<?> target)
public static TableSubject assertThat(@Nullable com.google.common.collect.Table<?,?,?> target)
public static AtomicLongMapSubject assertThat(@Nullable com.google.common.util.concurrent.AtomicLongMap<?> target)
Copyright © 2017. All rights reserved.