2017-07-26 35 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