Bird
Raised Fist0
JUnittesting~20 mins

Why assertions verify expected outcomes in JUnit - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Assertion Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate
2:00remaining
Understanding assertion failure in JUnit
What will happen when this JUnit assertion runs if the actual value is 5 but the expected value is 10?
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;

int expected = 10;
int actual = 5;
assertEquals(expected, actual);
AThe test throws a NullPointerException because values are not objects.
BThe test passes because actual and expected are both integers.
CThe test fails with an AssertionError because actual does not equal expected.
DThe test is skipped because assertEquals is not called inside a test method.
Attempts:
2 left
💡 Hint
Assertions check if actual and expected values match exactly.
🧠 Conceptual
intermediate
1:30remaining
Purpose of assertions in test cases
Why do we use assertions in software tests like JUnit tests?
ATo make the program run faster by skipping code.
BTo automatically check if the program behaves as expected and report failures.
CTo document the code without affecting test results.
DTo replace manual testing by users completely.
Attempts:
2 left
💡 Hint
Think about what happens when an assertion fails during a test.
❓ Predict Output
advanced
2:00remaining
Output of a failing JUnit assertion with message
What is the output when this JUnit assertion fails?
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;

String expected = "hello";
String actual = "world";
assertEquals(expected, actual, "Strings do not match!");
ATest fails with AssertionError and message: Strings do not match!
BTest throws compilation error due to wrong assertEquals usage.
CTest passes silently with no output.
DTest fails with NullPointerException because strings differ.
Attempts:
2 left
💡 Hint
Check how assertEquals behaves when expected and actual differ and a message is provided.
🔧 Debug
advanced
2:30remaining
Identify why this assertion does not verify correctly
Why does this assertion fail even though the lists contain the same elements?
JUnit
import java.util.List;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals;

List<String> expected = List.of("apple", "banana");
List<String> actual = new ArrayList<>();
actual.add("banana");
actual.add("apple");
assertEquals(expected, actual);
AThe assertion fails because the lists have different order of elements.
BThe assertion passes because both lists contain the same elements in different order, but List.equals requires order to match.
CThe assertion fails because actual is mutable and expected is immutable.
DThe assertion passes because List.equals checks only size, not order or content.
Attempts:
2 left
💡 Hint
Remember how List.equals works in Java regarding order.
❓ framework
expert
2:00remaining
How does JUnit handle assertion failures during test execution?
When a JUnit assertion fails inside a test method, what does the framework do next?
AJUnit ignores the failure and continues running the rest of the test method.
BJUnit stops all tests in the suite and exits immediately.
CJUnit retries the assertion automatically until it passes or times out.
DJUnit stops executing the current test method immediately and marks it as failed.
Attempts:
2 left
💡 Hint
Think about what happens when an exception is thrown inside a test method.

Practice

(1/5)
1. Why do we use assertions in JUnit tests?
easy
A. To pause the test execution
B. To print messages to the console
C. To create new objects in the test
D. To check if the actual output matches the expected output

Solution

  1. Step 1: Understand the purpose of assertions

    Assertions are used to compare the actual result of code with what we expect it to be.
  2. Step 2: Identify the correct use in JUnit

    JUnit assertions verify correctness by checking if actual equals expected, helping catch errors early.
  3. Final Answer:

    To check if the actual output matches the expected output -> Option D
  4. Quick Check:

    Assertions verify expected outcomes = A [OK]
Hint: Assertions compare actual vs expected results [OK]
Common Mistakes:
  • Thinking assertions print messages
  • Using assertions to create objects
  • Believing assertions pause tests
2. Which of the following is the correct syntax to assert that two integers are equal in JUnit?
easy
A. assertEquals(expected, actual);
B. assertEqual(actual, expected);
C. assertEquals(actual, expected);
D. assertEqual(expected, actual);

Solution

  1. Step 1: Recall JUnit assertion method signature

    The correct method is assertEquals(expected, actual) where expected value comes first.
  2. Step 2: Check each option's parameter order and method name

    Only assertEquals(expected, actual); uses assertEquals with expected first, matching JUnit syntax.
  3. Final Answer:

    assertEquals(expected, actual); -> Option A
  4. Quick Check:

    assertEquals(expected, actual) = B [OK]
Hint: Remember expected value comes first in assertEquals [OK]
Common Mistakes:
  • Swapping expected and actual parameters
  • Using assertEqual instead of assertEquals
  • Confusing parameter order
3. Consider this JUnit test snippet:
int result = calculator.add(2, 3);
assertEquals(5, result);

What happens if calculator.add(2, 3) returns 4 instead of 5?
medium
A. The test fails with an assertion error
B. The test passes successfully
C. The test throws a syntax error
D. The test is skipped

Solution

  1. Step 1: Understand assertion behavior on mismatch

    If actual result (4) does not equal expected (5), assertEquals fails and throws an error.
  2. Step 2: Identify test outcome

    JUnit reports a failure with an assertion error, indicating the test did not pass.
  3. Final Answer:

    The test fails with an assertion error -> Option A
  4. Quick Check:

    Mismatch causes failure = A [OK]
Hint: Assertion fails if actual ≠ expected [OK]
Common Mistakes:
  • Thinking test passes despite wrong result
  • Confusing assertion failure with syntax error
  • Assuming test is skipped automatically
4. Identify the error in this JUnit assertion:
assertEquals(result, 10);

Assuming result is an integer variable expected to be 10.
medium
A. assertEquals requires a message parameter
B. assertEquals cannot compare integers
C. The parameters are reversed; expected should be first
D. Missing semicolon at the end

Solution

  1. Step 1: Check parameter order in assertEquals

    JUnit expects assertEquals(expected, actual), but here actual is first.
  2. Step 2: Confirm other syntax elements

    Semicolon is present, integers can be compared, message is optional, so no other errors.
  3. Final Answer:

    The parameters are reversed; expected should be first -> Option C
  4. Quick Check:

    Expected first in assertEquals = D [OK]
Hint: Expected value always comes first in assertEquals [OK]
Common Mistakes:
  • Swapping expected and actual parameters
  • Thinking assertEquals needs a message
  • Assuming semicolon is missing
5. You want to verify a method returns a non-null string "Hello". Which assertion best verifies both conditions in one test?
hard
A. assertNotNull(result); assertEquals("Hello", result);
B. assertTrue(result != null && result.equals("Hello"));
C. assertEquals("Hello", result);
D. assertNull(result);

Solution

  1. Step 1: Understand the requirement

    We want to check result is not null and equals "Hello" in one assertion.
  2. Step 2: Evaluate each option

    assertNotNull(result); assertEquals("Hello", result); uses two assertions, not one. assertEquals("Hello", result); checks equality but not null explicitly. assertTrue(result != null && result.equals("Hello")); uses assertTrue with combined condition, verifying both at once. assertNull(result); checks for null, which is opposite.
  3. Final Answer:

    assertTrue(result != null && result.equals("Hello")); -> Option B
  4. Quick Check:

    Combine conditions in assertTrue = C [OK]
Hint: Use assertTrue with combined condition for multiple checks [OK]
Common Mistakes:
  • Using multiple assertions instead of one
  • Not checking for null explicitly
  • Using assertNull when expecting non-null
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