assertAll?import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class SampleTest { @Test void testGroupedAssertions() { assertAll("group", () -> assertEquals(2, 1 + 1), () -> assertTrue(3 > 2), () -> assertFalse(5 < 3) ); } }
The assertAll method runs all grouped assertions and reports any failures together after executing all. Since all assertions are true here, the test passes.
assertAll?import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class SampleTest { @Test void testFailures() { assertAll("failures", () -> assertEquals(5, 2 + 2, "Sum mismatch"), () -> assertTrue(1 > 2, "Condition failed"), () -> assertFalse(false, "Boolean check") ); } }
The first two assertions fail and their messages are reported. The third assertion assertFalse(false) passes, so its message is not reported.
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class SampleTest { @Test void testIncorrectAssertAll() { assertAll("group", assertEquals(1, 1), () -> assertTrue(true) ); } }
The first argument inside assertAll must be a lambda (Executable). Here, assertEquals(1, 1) is called immediately, returning void, causing a compilation error.
Using assertAll with lambdas is the recommended way to group assertions so all run and failures are reported together.
assertAll for grouped assertions in JUnit 5 compared to writing multiple separate assertions?The key benefit of assertAll is that it executes all assertions and reports all failures at once, giving better feedback than stopping at the first failure.
