您的代碼看起來有效。以下測試......
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
很奇怪,在控制檯上沒有出錯,但是集電極包含所有assertionerror,但不在控制檯上顯示...使用評估表達式工具,簡單的檢查結果是未定義的。 – brobee
我決定,我將其標記爲答案,但對我來說它不起作用。可能,我擴展了我的測試類,因爲它對我來說是必需的,在這種情況下,錯誤收集器不起作用。 – brobee