Challenge - 5 Problems
Assertion Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate2: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);
Attempts:
2 left
💡 Hint
Assertions check if actual and expected values match exactly.
✗ Incorrect
Assertions in JUnit compare expected and actual values. If they differ, the test fails with an AssertionError. Here, 5 is not equal to 10, so the assertion fails.
🧠 Conceptual
intermediate1:30remaining
Purpose of assertions in test cases
Why do we use assertions in software tests like JUnit tests?
Attempts:
2 left
💡 Hint
Think about what happens when an assertion fails during a test.
✗ Incorrect
Assertions verify that the program's output matches what we expect. If not, they cause the test to fail, helping us find bugs early.
❓ Predict Output
advanced2: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!");
Attempts:
2 left
💡 Hint
Check how assertEquals behaves when expected and actual differ and a message is provided.
✗ Incorrect
When assertEquals fails, it throws an AssertionError showing the provided message to explain the failure.
🔧 Debug
advanced2: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);
Attempts:
2 left
💡 Hint
Remember how List.equals works in Java regarding order.
✗ Incorrect
List.equals returns true only if both lists have the same elements in the same order. Here, order differs, so assertion fails.
❓ framework
expert2: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?
Attempts:
2 left
💡 Hint
Think about what happens when an exception is thrown inside a test method.
✗ Incorrect
When an assertion fails, it throws an AssertionError which stops the current test method execution and marks it as failed. Other tests continue normally.