0
0
JUnittesting~20 mins

Why assertions verify expected outcomes in JUnit - Challenge Your Understanding

Choose your learning style9 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.