Package com.google.common.truth
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
ATestRule
that batches up all failures encountered during a test, and reports them all together at the end (similar toErrorCollector
). 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:
@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:Expect
has 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 anyFuture
returned 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Statement
apply(Statement base, Description description)
static Expect
create()
Creates a new instance.boolean
hasFailures()
-
Methods inherited from class com.google.common.truth.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, withMessage
-
-