Package com.google.common.truth
Class Expect
- java.lang.Object
-
- com.google.common.truth.StandardSubjectBuilder
-
- com.google.common.truth.Expect
-
- All Implemented Interfaces:
org.junit.rules.TestRule
public final class Expect extends StandardSubjectBuilder implements org.junit.rules.TestRule
ATestRulethat 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:
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();Expectmay be used concurrently from multiple threads. Note, however, thatExpecthas 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.To record failures for the purpose of testing that an assertion fails when it should, see
ExpectFailure.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classExpect.ExpectationGatherer(package private) static classExpect.TestPhase
-
Field Summary
Fields Modifier and Type Field Description private Expect.ExpectationGatherergatherer
-
Constructor Summary
Constructors Modifier Constructor Description privateExpect(Expect.ExpectationGatherer gatherer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description org.junit.runners.model.Statementapply(org.junit.runners.model.Statement base, org.junit.runner.Description description)(package private) voidcheckStatePreconditions()Extension point invoked before every assertion.static Expectcreate()Creates a new instance.booleanhasFailures()-
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, withMessage, withMessage
-
-
-
-
Field Detail
-
gatherer
private final Expect.ExpectationGatherer gatherer
-
-
Constructor Detail
-
Expect
private Expect(Expect.ExpectationGatherer gatherer)
-
-
Method Detail
-
create
public static Expect create()
Creates a new instance.
-
hasFailures
public boolean hasFailures()
-
checkStatePreconditions
void checkStatePreconditions()
Description copied from class:StandardSubjectBuilderExtension point invoked before every assertion. This allowsExpectto check that it's been set up properly as aTestRule.- Overrides:
checkStatePreconditionsin classStandardSubjectBuilder
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)- Specified by:
applyin interfaceorg.junit.rules.TestRule
-
-