简体   繁体   中英

How to show all the failures in Junit Assertions

My requirement is to show all the failures after Junit test run.

I tried two things:

Assertions.assertEquals --> This stops the execution after first failure, and in test report I see only first failure.

Assertions.assertEquals(expceted, actual,"Error Message");

assertj.SoftAssertions --> Here execution happens but in test report I do not see any failure message.

SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThat(expected).withFailMessage("Error Message", actual) ;
                   

Any Idea if any other type of assertion or any other option I can use with these assertions?

Thanks in advance !!!

JUnit 5 added assertAll :

assertAll(
        () -> assertEquals(...),
        () -> assertTrue(...)
        // etc
);

It takes any number of lambdas, and if I recall correctly each lambda can even throw exceptions. That means you can delegate to methods:

assertAll(
        ...
        complexAssertion(...)
);

...

private void complexAssertion(...) throws IOException {
    String expectedOutput = <read resource>;
    SomeException thrown = assertThrows(SomeException.class, () -> ...);
    assertInstanceOf(SomeOtherException.class, thrown.getCause());
    // nested assertAll can also be used!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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