Class ErrorCollector

java.lang.Object
org.junit.rules.Verifier
org.junit.rules.ErrorCollector
All Implemented Interfaces:
TestRule

public class ErrorCollector extends Verifier
The ErrorCollector rule allows execution of a test to continue after the first problem is found (for example, to collect _all_ the incorrect rows in a table, and report them all at once):
 public static class UsesErrorCollectorTwice {
        @Rule
        public ErrorCollector collector= new ErrorCollector();

 @Test
 public void example() {
      collector.addError(new Throwable("first thing went wrong"));
      collector.addError(new Throwable("second thing went wrong"));
      collector.checkThat(getResult(), not(containsString("ERROR!")));
      // all lines will run, and then a combined failure logged at the end.
     }
 }
 
Since:
4.7
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private List<Throwable>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a Throwable to the table.
    <T> T
    checkSucceeds(Callable<T> callable)
    Adds to the table the exception, if any, thrown from callable.
    <T> void
    checkThat(String reason, T value, org.hamcrest.Matcher<T> matcher)
    Adds a failure with the given reason to the table if matcher does not match value.
    <T> void
    checkThat(T value, org.hamcrest.Matcher<T> matcher)
    Adds a failure to the table if matcher does not match value.
    void
    checkThrows(Class<? extends Throwable> expectedThrowable, ThrowingRunnable runnable)
    Adds a failure to the table if runnable does not throw an exception of type expectedThrowable when executed.
    protected void
    Override this to add verification logic.

    Methods inherited from class org.junit.rules.Verifier

    apply

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • ErrorCollector

      public ErrorCollector()
  • Method Details

    • verify

      protected void verify() throws Throwable
      Description copied from class: Verifier
      Override this to add verification logic. Overrides should throw an exception to indicate that verification failed.
      Overrides:
      verify in class Verifier
      Throws:
      Throwable
    • addError

      public void addError(Throwable error)
      Adds a Throwable to the table. Execution continues, but the test will fail at the end.
    • checkThat

      public <T> void checkThat(T value, org.hamcrest.Matcher<T> matcher)
      Adds a failure to the table if matcher does not match value. Execution continues, but the test will fail at the end if the match fails.
    • checkThat

      public <T> void checkThat(String reason, T value, org.hamcrest.Matcher<T> matcher)
      Adds a failure with the given reason to the table if matcher does not match value. Execution continues, but the test will fail at the end if the match fails.
    • checkSucceeds

      public <T> T checkSucceeds(Callable<T> callable)
      Adds to the table the exception, if any, thrown from callable. Execution continues, but the test will fail at the end if callable threw an exception.
    • checkThrows

      public void checkThrows(Class<? extends Throwable> expectedThrowable, ThrowingRunnable runnable)
      Adds a failure to the table if runnable does not throw an exception of type expectedThrowable when executed. Execution continues, but the test will fail at the end if the runnable does not throw an exception, or if it throws a different exception.
      Parameters:
      expectedThrowable - the expected type of the exception
      runnable - a function that is expected to throw an exception when executed
      Since:
      4.13