2

我有几个断言,我想收集所有问题,最后整个测试应该失败,控制台输出正确。但是现在,我什么都没有。

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

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

问题是什么?

谢谢!

4

2 回答 2

3

如果您使用的是 Junit 5 (Jupiter),则需要以下允许@ErrorCollector规则工作的附加依赖项:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-migrationsupport</artifactId>
        <version>${jupiter.version}</version>
        <scope>test</scope>
    </dependency>

之后,您需要@EnableRuleMigrationSupport在测试类级别包含注释。

于 2020-05-14T18:08:36.987 回答
2

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

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 进行了验证

于 2017-07-27T08:06:02.907 回答