Class Expect

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public final class Expect
    extends StandardSubjectBuilder
    implements org.junit.rules.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:

     @Rule public final Expect expect = Expect.create();
    
     ...
    
       expect.that(results).containsExactly(...);
       expect.that(errors).isEmpty();
     
    If both of the assertions above fail, the test will fail with an exception that contains information about both.

    Expect may be used concurrently from multiple threads. Note, however, that 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.

    To record failures for the purpose of testing that an assertion fails when it should, see ExpectFailure.

    • Method Detail

      • create

        public static Expect create()
        Creates a new instance.
      • hasFailures

        public boolean hasFailures()
      • apply

        public org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement base,
                                                       org.junit.runner.Description description)
        Specified by:
        apply in interface org.junit.rules.TestRule