Issue
There was the ErrorCollector Rule in JUnit4 and now we have to switch to extensions during migration to JUnit5. Usage of the ErrorCollector was described here https://junit.org/junit4/javadoc/4.12/org/junit/rules/ErrorCollector.html Is there a similar extension in JUnit5. I found one in the assert-j https://www.javadoc.io/doc/org.assertj/assertj-core/latest/org/assertj/core/api/junit/jupiter/SoftAssertionsExtension.html but is the same thing still supported in JUnit 5 as an extension?
Note: I would like to use this on a system testing level. So I would have Step 1 -> assertion -> Step 2-> assertion->... assertAll in my opinion is worse option here as I have to store values for verification and assert them at the end of the test, not in places where I got these values.
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"));
This approach doesn't look clear and looks worse than described here https://assertj.github.io/doc/#assertj-core-junit5-soft-assertions
Solution
For now I see that the best way is to use assert-j like this
@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});
...
}
}
or
@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});
...
}
}
It looks more understandable then to write many steps with verification in one line
Answered By - Andrii Olieinik