Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Junit ErrorCollector用法-未显示故障_Java_Junit_Junit4_Errorcollector - Fatal编程技术网

Java Junit ErrorCollector用法-未显示故障

Java Junit ErrorCollector用法-未显示故障,java,junit,junit4,errorcollector,Java,Junit,Junit4,Errorcollector,我有几个断言,我想收集所有的问题,最后整个测试应该失败,并有适当的控制台输出。但是现在,我什么都没有 @Rule public ErrorCollector collector = new ErrorCollector(); Matcher<Boolean> matchesTrue = IsEqual.equalTo(true); collector.checkThat("FAILURE","BLA".equals("OK"), matchesTrue);

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

    @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);
@规则
public ErrorCollector=新ErrorCollector();
Matcher matchesTrue=等当量(真);
collector.checkThat(“FAILURE”,“BLA.”等于(“OK”),matchesTrue);
collector.checkThat(“FAILURE”,“BLABLA.”等于(“OK”),matchesTrue);
运行后,控制台上的所有内容都是绿色的,没有错误

有什么问题


谢谢

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

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);
  }
}
公共类ErrorCollectorTest{
@统治
public ErrorCollector=新ErrorCollector();
@试验
public void testErrorCollection(){
org.hamcrest.Matcher matchesTrue=org.hamcrest.core.IsEqual.equalTo(true);
collector.checkThat(“FAILURE”,“BLA.”等于(“OK”),matchesTrue);
collector.checkThat(“FAILURE”,“BLABLA.”等于(“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)
    ...
java.lang.AssertionError:失败
预期:
但是:是吗
位于org.hamcrest.matcherasert.assertThat(matcherasert.java:20)
位于org.junit.Assert.assertThat(Assert.java:956)
在org.junit.rules.ErrorCollector$1.call(ErrorCollector.java:65)
在org.junit.rules.ErrorCollector.checkSuccesses(ErrorCollector.java:78)
在org.junit.rules.ErrorCollector.checkThat(ErrorCollector.java:63)
...
java.lang.AssertionError:失败
预期:
但是:是吗
位于org.hamcrest.matcherasert.assertThat(matcherasert.java:20)
位于org.junit.Assert.assertThat(Assert.java:956)
在org.junit.rules.ErrorCollector$1.call(ErrorCollector.java:65)
在org.junit.rules.ErrorCollector.checkSuccesses(ErrorCollector.java:78)
在org.junit.rules.ErrorCollector.checkThat(ErrorCollector.java:63)
...
JUnit 4.12和Hamcrest对此进行了验证。如果您使用的是JUnit 5(Jupiter),则需要以下附加依赖项,以允许
@ErrorCollector
规则工作:

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

org.junit.jupiter
junit jupiter迁移支持
${jupiter.version}
测试

接下来,您需要在测试类级别包含
@EnableRuleMigrationSupport
注释。

非常奇怪,控制台上没有错误,但是收集器包含所有assertionerror,但控制台上没有显示。。。使用evaluate expression工具,简单地检查结果是否未定义。我决定,我将其标记为glitch作为答案,但对我来说,它不起作用。也许,我扩展了我的测试类,因为它对我来说是必要的,在这种情况下,错误收集器不工作。它工作了。但是对于扩展类,我们需要从父测试类传递errorCollector对象。