Class Expect
java.lang.Object
com.google.common.truth.StandardSubjectBuilder
com.google.common.truth.Expect
- All Implemented Interfaces:
TestRule
@GwtIncompatible("JUnit4")
public final class Expect
extends StandardSubjectBuilder
implements TestRule
A
TestRule that batches up all failures encountered during a test, and reports them all
together at the end (similar to ErrorCollector). It is also useful for making assertions
from other threads or from within callbacks whose exceptions would be swallowed or logged, rather
than propagated out to fail the test. (AssertJ has a similar feature called "soft assertions"; however, soft
assertions are not safe for concurrent use.)
Usage:
If both of the assertions above fail, the test will fail with an exception that contains information about both.@Rule public final Expect expect = Expect.create();...expect.that(results).containsExactly(...);expect.that(errors).isEmpty();
Expect may be used concurrently from multiple threads. However, multithreaded tests
still require care:
Expecthas no way of knowing when all your other test threads are done. It simply checks for failures when the main thread finishes executing the test method. Thus, you must ensure that any background threads complete their assertions before then, or your test may ignore their results.- Assertion failures are not the only exceptions that may occur in other threads. For maximum
safety, multithreaded tests should check for such exceptions regardless of whether they use
Expect. (Typically, this means callingget()on anyFuturereturned by a method likeexecutor.submit(...). It might also include checking for unexpected log messages or reading metrics that count failures.) If your tests already check for exceptions from a thread, then that will cover any exception from plainassertThat.
To record failures for the purpose of testing that an assertion fails when it should, see
ExpectFailure.
For more on this class, see the documentation page.
-
Method Summary
Modifier and TypeMethodDescriptionapply(Statement base, Description description) static Expectcreate()Creates a new instance.booleanMethods inherited from class StandardSubjectBuilder
about, about, fail, forCustomFailureStrategy, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, that, withMessage, withMessageModifier and TypeMethodDescriptionfinal <CustomSubjectBuilderT extends CustomSubjectBuilder>
CustomSubjectBuilderTabout(CustomSubjectBuilder.Factory<CustomSubjectBuilderT> factory) A generic, advanced method of extension of Truth to new types, which is documented onCustomSubjectBuilder.final <S extends Subject, A>
SimpleSubjectBuilder<S, A> about(Subject.Factory<S, A> factory) Given a factory for someSubjectclass, returns a builder whosethat(actual)method creates instances of that class.final voidfail()Reports a failure.static StandardSubjectBuilderforCustomFailureStrategy(FailureStrategy strategy) Returns a new instance that invokes the givenFailureStrategywhen a check fails.final PrimitiveIntArraySubjectfinal <ComparableT extends Comparable<?>>
ComparableSubject<ComparableT> final GuavaOptionalSubjectfinal MultimapSubjectfinal MultisetSubjectfinal TableSubjectfinal BooleanSubjectfinal ClassSubjectfinal DoubleSubjectfinal FloatSubjectfinal IntegerSubjectfinal IterableSubjectfinal LongSubjectfinal Subjectfinal StringSubjectfinal ThrowableSubjectfinal BigDecimalSubjectthat(@Nullable BigDecimal actual) final PathSubjectfinal MapSubjectfinal OptionalSubjectfinal OptionalDoubleSubjectthat(@Nullable OptionalDouble actual) final OptionalIntSubjectthat(@Nullable OptionalInt actual) final OptionalLongSubjectthat(@Nullable OptionalLong actual) final IntStreamSubjectfinal LongStreamSubjectthat(@Nullable LongStream actual) final StreamSubjectfinal <T extends @Nullable Object>
ObjectArraySubject<T> final StandardSubjectBuilderwithMessage(@Nullable String message) Returns a new instance that will output the given message before the main failure message.final StandardSubjectBuilderwithMessage(String format, @Nullable Object... args) Returns a new instance that will output the given message before the main failure message.