Bird
Raised Fist0
JUnittesting~20 mins

assertAll for grouped assertions in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
AssertAll Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
        );
    }
}
ATest fails immediately at the first failed assertion inside assertAll.
BTest throws a runtime exception due to incorrect assertAll usage.
CTest fails but only reports the first failed assertion, ignoring others.
DTest passes with all assertions checked and no failures.
Attempts:
2 left
💡 Hint
assertAll runs all assertions and reports all failures together.
❓ assertion
intermediate
2: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")
        );
    }
}
AReports all three failure messages: "Sum mismatch", "Condition failed", and "Boolean check".
BReports failure: "Boolean check" only.
CReports failures: "Sum mismatch" and "Condition failed" only.
DNo failures reported; test passes.
Attempts:
2 left
💡 Hint
Check which assertions actually fail.
🔧 Debug
advanced
2: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)
        );
    }
}
ACompilation error: incompatible types, expected Executable but found void.
BTest fails at runtime with AssertionError.
CTest passes successfully without errors.
DNullPointerException at runtime.
Attempts:
2 left
💡 Hint
Check the lambda expressions passed to assertAll.
❓ framework
advanced
2: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?
AUse multiple separate assertions without assertAll; test stops at first failure.
BUse assertAll with lambda expressions for each assertion inside a single group.
CUse try-catch blocks around each assertion to catch failures manually.
DUse a custom loop to run assertions and collect failures manually.
Attempts:
2 left
💡 Hint
assertAll is designed for grouped assertions.
🧠 Conceptual
expert
2: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?
AassertAll runs all assertions and reports all failures together, improving test feedback.
BassertAll runs assertions sequentially but stops at the first failure to save time.
CassertAll automatically retries failed assertions to reduce flaky tests.
DassertAll converts all assertions into warnings instead of failures.
Attempts:
2 left
💡 Hint
Think about how test failures are reported.

Practice

(1/5)
1. What is the main benefit of using assertAll in JUnit tests?
easy
A. It runs multiple assertions and reports all failures together.
B. It stops the test immediately after the first failure.
C. It automatically fixes failed assertions.
D. It runs assertions in parallel threads.

Solution

  1. Step 1: Understand assertAll behavior

    assertAll groups multiple assertions and executes them all, regardless of failures.
  2. Step 2: Compare options

    It runs multiple assertions and reports all failures together. correctly states that all failures are reported together. Options A, C, and D are incorrect because assertAll does not stop early, fix errors, or run assertions in parallel.
  3. Final Answer:

    It runs multiple assertions and reports all failures together. -> Option A
  4. Quick Check:

    assertAll reports all failures together = B [OK]
Hint: Remember: assertAll shows all errors, not just first [OK]
Common Mistakes:
  • Thinking assertAll stops after first failure
  • Believing assertAll fixes errors automatically
  • Assuming assertAll runs assertions in parallel
2. Which of the following is the correct syntax to use assertAll in JUnit 5?
easy
A. assertAll(() -> assertEquals(5, sum), () -> assertTrue(flag));
B. assertAll(assertEquals(5, sum), assertTrue(flag));
C. assertAll{ assertEquals(5, sum); assertTrue(flag); }
D. assertAll[() -> assertEquals(5, sum), () -> assertTrue(flag)];

Solution

  1. Step 1: Recall assertAll syntax

    In JUnit 5, assertAll takes lambda expressions as arguments, each representing an assertion.
  2. Step 2: Check options

    assertAll(() -> assertEquals(5, sum), () -> assertTrue(flag)); correctly uses lambdas with parentheses. Options A, B, and D use incorrect syntax like missing lambdas, braces, or wrong brackets.
  3. Final Answer:

    assertAll(() -> assertEquals(5, sum), () -> assertTrue(flag)); -> Option A
  4. Quick Check:

    assertAll uses lambdas with parentheses = C [OK]
Hint: Use lambdas inside parentheses for assertAll [OK]
Common Mistakes:
  • Omitting lambda arrows () ->
  • Using curly braces instead of parentheses
  • Passing assertions directly without lambdas
3. Given the code below, what will be the test result?
assertAll(
  () -> assertEquals(2, 1 + 1),
  () -> assertTrue(3 > 5),
  () -> assertFalse(false)
);
medium
A. Test fails showing only the second assertion failure.
B. Test fails showing failures for the second assertion only, but all assertions run.
C. Test fails showing failures for the second assertion only.
D. Test passes because two assertions are true.

Solution

  1. Step 1: Evaluate each assertion

    First assertion: 1+1 equals 2, passes. Second: 3 > 5 is false, fails. Third: assertFalse(false) passes.
  2. Step 2: Understand assertAll behavior

    All assertions run. The test fails because one assertion failed. The failure report shows the second assertion failure only.
  3. Final Answer:

    Test fails showing failures for the second assertion only, but all assertions run. -> Option B
  4. Quick Check:

    assertAll runs all, reports all failures = D [OK]
Hint: assertAll runs all assertions, reports all failures [OK]
Common Mistakes:
  • Thinking test passes if some assertions pass
  • Believing assertAll stops at first failure
  • Expecting multiple failure messages when only one fails
4. Identify the error in the following JUnit 5 test code using assertAll:
assertAll(
  () -> assertEquals(10, calculate()),
  assertTrue(isReady())
);
medium
A. assertAll cannot take more than one assertion.
B. assertEquals should be replaced with assertSame.
C. Missing lambda expression for the second assertion.
D. assertTrue should be inside curly braces.

Solution

  1. Step 1: Check assertAll argument types

    Each argument to assertAll must be a lambda expression or executable.
  2. Step 2: Identify the problem

    The second argument assertTrue(isReady()) is a method call, not a lambda. It should be wrapped as () -> assertTrue(isReady()).
  3. Final Answer:

    Missing lambda expression for the second assertion. -> Option C
  4. Quick Check:

    assertAll needs lambdas for each assertion = A [OK]
Hint: Wrap each assertion in () -> for assertAll [OK]
Common Mistakes:
  • Passing assertions directly without lambdas
  • Confusing assertEquals with assertSame
  • Using curly braces instead of lambdas
5. You want to test a Person object's fields name, age, and email together using assertAll. Which code snippet correctly groups these assertions and reports all failures at once?
Person p = new Person("Alice", 30, "alice@example.com");
// Choose the correct assertAll usage:
hard
A. assertAll[ () -> assertEquals("Alice", p.getName()), () -> assertEquals(30, p.getAge()), () -> assertEquals("alice@example.com", p.getEmail()) ];
B. assertAll( assertEquals("Alice", p.getName()), assertEquals(30, p.getAge()), assertEquals("alice@example.com", p.getEmail()) );
C. assertAll(() -> { assertEquals("Alice", p.getName()); assertEquals(30, p.getAge()); assertEquals("alice@example.com", p.getEmail()); });
D. assertAll( () -> assertEquals("Alice", p.getName()), () -> assertEquals(30, p.getAge()), () -> assertEquals("alice@example.com", p.getEmail()) );

Solution

  1. Step 1: Understand assertAll argument requirements

    assertAll expects multiple lambda expressions as separate arguments, each containing one assertion.
  2. Step 2: Evaluate each option

    assertAll( () -> assertEquals("Alice", p.getName()), () -> assertEquals(30, p.getAge()), () -> assertEquals("alice@example.com", p.getEmail()) ); correctly passes three lambdas as separate arguments. assertAll( assertEquals("Alice", p.getName()), assertEquals(30, p.getAge()), assertEquals("alice@example.com", p.getEmail()) ); passes method calls directly, which is invalid. assertAll(() -> { assertEquals("Alice", p.getName()); assertEquals(30, p.getAge()); assertEquals("alice@example.com", p.getEmail()); }); passes a single lambda with multiple assertions inside, which does not report all failures separately. assertAll[ () -> assertEquals("Alice", p.getName()), () -> assertEquals(30, p.getAge()), () -> assertEquals("alice@example.com", p.getEmail()) ]; uses square brackets, which is invalid syntax.
  3. Final Answer:

    assertAll( () -> assertEquals("Alice", p.getName()), () -> assertEquals(30, p.getAge()), () -> assertEquals("alice@example.com", p.getEmail()) ); -> Option D
  4. Quick Check:

    assertAll needs separate lambdas for each assertion = A [OK]
Hint: Use separate lambdas for each assertion inside assertAll [OK]
Common Mistakes:
  • Passing assertions directly without lambdas
  • Using one lambda with multiple assertions inside
  • Using wrong brackets like [] instead of ()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard