您的代码看起来有效。以下测试......
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