当前位置: 首页 > 知识库问答 >
问题:

JUnit5是否有类似的错误收集器规则

申高峯
2023-03-14

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

注意:我想在系统测试级别上使用它。所以我要做第一步-

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

共有2个答案

贡光明
2023-03-14

目前,我认为最好的方法是像这样使用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});
        ...
    }
}

它看起来更容易理解然后写许多步骤验证在一行

商松
2023-03-14

木星的Asertall距离我们最近:https://junit.org/junit5/docs/current/user-guide/#writing-测试断言。

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

@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())
    );
}
 类似资料:
  • 不保留顺序。我可以使用列表,但我想指出,生成的集合不允许元素重复,这正是接口的用途。

  • 问题内容: 我喜欢整个WMI概念,并且可以在Linux(在某些脚本中)中真正使用它。Linux系统有类似的东西吗? 问题答案: 并不是的。您是否正在使用WMI获取系统参数,查询过程,更改配置或监视系统事件,等等? 内核通过和文件系统公开了许多信息和可调旋钮。没有查询语言,只有目录和文件的组织层次结构。其中一些文件是只读,读写或只写的。其中一些人有能力。 有些服务可能具有动态自定义客户查询和更新配置

  • 我尝试保存“统一差异”,但找不到应用它的方法。 补丁适用于提交的更改,我不希望在没有适当代码审查的情况下提交。 点子赞赏!

  • 我想像这样在中使用一个不可序列化的对象 它非常低效,因为我创建了许多实例。实际上,它只能在每个工作器中创建一次。 在Spark中,我可以使用mapPartition来执行此操作。但在flink stream api中,我不知道。

  • 我正在尝试使用JUnit错误收集器报告错误。虽然我的断言失败了,但JUnit中并没有报告错误。但我在控制台中收到了“错误”信息。

  • 问题内容: 在C#中,您可以将一个类标记为,以便只能从同一包中对其进行访问。Java有什么类似的东西吗? 问题答案: 您可以通过从类的声明中省略安全修饰符(公共,私有)来创建程序包专用类。