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
木星的 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});
...
}
}
那么一行写很多步骤加验证比较好理解
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
木星的 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});
...
}
}
那么一行写很多步骤加验证比较好理解