The interesting thing about assertAll is that it always checks all the statements passed to it , no matter how many failures. If everything goes well, everything is fine - if at least one of them fails, you will get a detailed result of everything that went wrong (and, for that matter).
It is best used to validate a set of properties that are conceptually combined. Something where your first instinct will be: "I want to assert this as one."
Example
Your specific example is not the best use case for assertAll , because checking isPrime using simple and non-simple is independent of each other - so much so that I would recommend writing two testing methods for this.
But suppose you have a simple class, such as an address with the city , street , number fields, and would like to state that this is what you expect from them:
Address address = unitUnderTest.methodUnderTest(); assertEquals("Redwood Shores", address.getCity()); assertEquals("Oracle Parkway", address.getStreet()); assertEquals("500", address.getNumber());
Now, as soon as the first statement fails, you will never see the results of the second, which can be very annoying. There are many ways, and JUnit Jupiter assertAll is one of them:
Address address = unitUnderTest.methodUnderTest(); assertAll("Should return address of Oracle headquarter", () -> assertEquals("Redwood Shores", address.getCity()), () -> assertEquals("Oracle Parkway", address.getStreet()), () -> assertEquals("500", address.getNumber()) );
If the test method returns an invalid address, this is an error:
org.opentest4j.MultipleFailuresError: Should return address of Oracle headquarter (3 failures) expected: <Redwood Shores> but was: <Walldorf> expected: <Oracle Parkway> but was: <Dietmar-Hopp-Allee> expected: <500> but was: <16>