我有几个断言,我想收集所有问题,最后整个测试应该失败,并提供适当的控制台输出。但是现在,我什么都没有。
@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);
运行后,一切都是绿色的,控制台上没有错误。
有什么问题?
谢谢!
答案 0 :(得分:2)
如果您使用的是Junit 5(Jupiter),则需要以下附加依赖项,以使@ErrorCollector
规则起作用:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>${jupiter.version}</version>
<scope>test</scope>
</dependency>
接着,您需要在测试课程级别添加@EnableRuleMigrationSupport
注释。
答案 1 :(得分: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
验证