繁体   English   中英

是否有 JUnit5 的 ErrorCollector 规则模拟

[英]Is there ErrorCollector rule analogue for JUnit5

JUnit4 中有 ErrorCollector 规则,现在我们必须在迁移到 JUnit5 期间切换到扩展。 此处描述了 ErrorCollector 的用法https://junit.org/junit4/javadoc/4.12/org/junit/rules/ErrorCollector.html JUnit5 中是否有类似的扩展。 我在 assert-j https://www.javadoc.io/doc/org.assertj/assertj-core/latest/org/assertj/core/api/junit/jupiter/SoftAssertionsExtension.html 中找到了一个但是是一样的JUnit 5 中仍然支持作为扩展吗?

注意:我想在系统测试级别使用它。 所以我会有 Step 1 -> assertion -> Step 2-> assertion->... assertAll 在我看来是更糟糕的选择,因为我必须存储验证值并在测试结束时断言它们,而不是在某些地方我从哪里得到这些值。

assertAll(() -> {{Some block of code getting variable2}
                    assertEquals({what we expect from variable1}, variable1, "variable1 is wrong")},
                {Some block of code getting variable2}
        assertEquals({what we expect from variable2}, variable2, "variable2 is wrong"),
                {Some block of code getting variable3}
        assertEquals({what we expect from variable3}, variable3, "variable3 is wrong"));

这种方法看起来不清晰,看起来比这里描述的更糟糕https://assertj.github.io/doc/#assertj-core-junit5-soft-assertions

Jupiter 的 aasertAll 最接近: https ://junit.org/junit5/docs/current/user-guide/#writing-tests-assertions。

它允许执行多个断言语句并一起报告它们的结果。 例如:

@Test
void groupedAssertions() {
    // In a grouped assertion all assertions are executed, and all
    // failures will be reported together.
    assertAll("person",
        () -> assertEquals("Jane", person.getFirstName()),
        () -> assertEquals("Doe", person.getLastName())
    );
}

现在我看到最好的方法是像这样使用 assert-j

@ExtendWith(SoftAssertionsExtension.class)
public class SoftAssertionsAssertJBDDTest {

    @InjectSoftAssertions
    BDDSoftAssertions bdd;

    @Test
    public void soft_assertions_extension_bdd_test() {
        //Some block of code getting variable1
        bdd.then(variable1).as("variable1 is wrong").isEqualTo({what we expect from variable1});
        //Some block of code getting variable2
        bdd.then(variable2).as("variable2 is wrong").isEqualTo({what we expect from variable2});
        //Some block of code getting variable3
        bdd.then(variable3).as("variable3 is wrong").isEqualTo({what we expect from variable3});
        ...
    }
}

要么

@ExtendWith(SoftAssertionsExtension.class)
public class SoftAssertionsAssertJTest {

    @Test
    public void soft_assertions_extension_test(SoftAssertions softly) {
        //Some block of code getting variable1
        softly.assertThat(variable1).as("variable1 is wrong").isEqualTo({what we expect from variable1});
        //Some block of code getting variable2
        softly.assertThat(variable2).as("variable2 is wrong").isEqualTo({what we expect from variable2});
        //Some block of code getting variable3
        softly.assertThat(variable3).as("variable3 is wrong").isEqualTo({what we expect from variable3});
        ...
    }
}

看起来比在一行中编写许多带有验证的步骤更容易理解

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM