Challenge - 5 Problems
AssertAll Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of grouped assertions with assertAll
What is the test execution result of the following JUnit code snippet using
assertAll?JUnit
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) ); } }
Attempts:
2 left
💡 Hint
assertAll runs all assertions and reports all failures together.
✗ Incorrect
The assertAll method runs all grouped assertions and reports any failures together after executing all. Since all assertions are true here, the test passes.
❓ assertion
intermediate2:00remaining
Which assertion failure messages are reported by assertAll?
Given the following JUnit test, which assertion failure messages will be reported after running
assertAll?JUnit
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") ); } }
Attempts:
2 left
💡 Hint
Check which assertions actually fail.
✗ Incorrect
The first two assertions fail and their messages are reported. The third assertion assertFalse(false) passes, so its message is not reported.
🔧 Debug
advanced2:00remaining
Identify the error in assertAll usage
What error will occur when running this JUnit test with incorrect assertAll usage?
JUnit
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) ); } }
Attempts:
2 left
💡 Hint
Check the lambda expressions passed to assertAll.
✗ Incorrect
The first argument inside assertAll must be a lambda (Executable). Here, assertEquals(1, 1) is called immediately, returning void, causing a compilation error.
❓ framework
advanced2:00remaining
Best practice for grouping assertions in JUnit 5
Which of the following is the best practice for grouping multiple assertions in JUnit 5 to ensure all assertions run and failures are reported together?
Attempts:
2 left
💡 Hint
assertAll is designed for grouped assertions.
✗ Incorrect
Using assertAll with lambdas is the recommended way to group assertions so all run and failures are reported together.
🧠 Conceptual
expert2:00remaining
Why use assertAll instead of multiple separate assertions?
What is the main advantage of using
assertAll for grouped assertions in JUnit 5 compared to writing multiple separate assertions?Attempts:
2 left
💡 Hint
Think about how test failures are reported.
✗ Incorrect
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.