2017-07-26 32 views
0

我有几个断言,我想收集所有的问题,最后整个测试应该失败,适当的控制台输出。但现在,我什么都没有。Junit ErrorCollector使用 - 失败不显示

@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); 

运行后,一切都呈绿色,控制台上没有错误。

什么问题?

谢谢!

回答

1

您的代码看起来有效。以下测试......

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); 
    } 
} 

...产生这样的输出:

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) 
    ... 

这验证了使用JUnit 4.12和Hamcrest

+0

很奇怪,在控制台上没有出错,但是集电极包含所有assertionerror,但不在控制台上显示...使用评估表达式工具,简单的检查结果是未定义的。 – brobee

+0

我决定,我将其标记为答案,但对我来说它不起作用。可能,我扩展了我的测试类,因为它对我来说是必需的,在这种情况下,错误收集器不起作用。 – brobee