1

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

  • As a migration step, you should actually be able to use the `ErrorCollector` rule unmodified with JUnit Jupiter, since Jupiter's migration support has support for `Verifier` which `ErrorCollector` extends. See https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-rule-support – Sam Brannen Jul 28 '21 at 15:07
  • Though I agree with the posted answer that using soft assertions from AssertJ is likely the cleanest solution. – Sam Brannen Jul 28 '21 at 15:09

2 Answers2

2

Jupiter‘s aasertAll comes closest: https://junit.org/junit5/docs/current/user-guide/#writing-tests-assertions.

It allows to execute several assertion statements and report their result together. Eg:

@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())
    );
}
johanneslink
  • 4,877
  • 1
  • 20
  • 37
  • It is different thing. I would like to use this on a system testing level. So I would have Step 1 -> assertion -> Step 2-> assertion->... – Andrii Olieinik Jul 24 '21 at 10:37
  • Since each lambda expression can do anything I don't see why you can't use assertAll( () -> {step1; assertion}, () -> {step2; assertion} ). AFAIK the lambdas will be executed in order. – johanneslink Jul 24 '21 at 14:18
  • You would have very long not understandable string of code. Especially if every step is a block of code. – Andrii Olieinik Jul 24 '21 at 21:10
2

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