Search code examples
javajunitjunit4errorcollector

Junit ErrorCollector usage - failure does not show


I have several assertion and I would like to collect all the problems and finally the whole test should fail with proper console output. But right now, I do not have anything.

    @Rule
    public ErrorCollector collector = new ErrorCollector();
    Matcher<Boolean> matchesTrue = IsEqual.equalTo(true);

collector.checkThat("FAILURE","BLA".equals("OK"), matchesTrue);
collector.checkThat("FAILURE","BLABLA".equals("OK"), matchesTrue);

After the run, everything is green and no error on the console.

What is the problem?

Thanks!


Solution

  • Your code looks valid. The following test ...

    public class ErrorCollectorTest {
    
      @Rule
      public ErrorCollector collector = new ErrorCollector();
    
      @Test
      public void testErrorCollection() {
        org.hamcrest.Matcher<Boolean> matchesTrue = org.hamcrest.core.IsEqual.equalTo(true);
    
        collector.checkThat("FAILURE", "BLA".equals("OK"), matchesTrue);
        collector.checkThat("FAILURE", "BLABLA".equals("OK"), matchesTrue);
      }
    }
    

    ... produces this output:

    java.lang.AssertionError: FAILURE
    Expected: <true>
         but: was <false>
    
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
        at org.junit.Assert.assertThat(Assert.java:956)
        at org.junit.rules.ErrorCollector$1.call(ErrorCollector.java:65)
        at org.junit.rules.ErrorCollector.checkSucceeds(ErrorCollector.java:78)
        at org.junit.rules.ErrorCollector.checkThat(ErrorCollector.java:63)
        ...
    
    
    java.lang.AssertionError: FAILURE
    Expected: <true>
         but: was <false>
    
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
        at org.junit.Assert.assertThat(Assert.java:956)
        at org.junit.rules.ErrorCollector$1.call(ErrorCollector.java:65)
        at org.junit.rules.ErrorCollector.checkSucceeds(ErrorCollector.java:78)
        at org.junit.rules.ErrorCollector.checkThat(ErrorCollector.java:63)
        ...
    

    This is verified with JUnit 4.12 and Hamcrest