0
0
JUnittesting~20 mins

Why exception testing validates error handling in JUnit - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this JUnit test when the method throws the expected exception?
Consider this JUnit 5 test method that expects an IllegalArgumentException. What will be the test result when the tested method throws this exception?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ExampleTest {
    @Test
    void testException() {
        assertThrows(IllegalArgumentException.class, () -> {
            throw new IllegalArgumentException("Invalid argument");
        });
    }
}
ATest passes because the expected exception was thrown.
BTest fails because the exception message does not match exactly.
CTest fails because no exception was thrown.
DTest passes but logs a warning about the exception.
Attempts:
2 left
💡 Hint
Remember that assertThrows expects the specified exception type to be thrown for the test to pass.
assertion
intermediate
2:00remaining
Which assertion correctly verifies that a method throws NullPointerException?
You want to write a JUnit 5 test that confirms a method throws NullPointerException when called with null. Which assertion is correct?
AassertTrue(method(null) instanceof NullPointerException);
BassertEquals(NullPointerException.class, method(null));
CassertThrows(NullPointerException.class, () -> method(null));
DassertDoesNotThrow(() -> method(null));
Attempts:
2 left
💡 Hint
Use the assertion designed to check for exceptions.
🧠 Conceptual
advanced
2:00remaining
Why is exception testing important for validating error handling?
Which statement best explains why exception testing is crucial in software testing?
AIt ensures the program crashes immediately on errors.
BIt replaces the need for functional testing of normal behavior.
CIt prevents any exceptions from ever occurring during execution.
DIt verifies that the program handles error conditions gracefully by throwing expected exceptions.
Attempts:
2 left
💡 Hint
Think about how programs should behave when something goes wrong.
🔧 Debug
advanced
2:00remaining
Identify the problem in this exception test code
This JUnit 5 test is supposed to check that a method throws IndexOutOfBoundsException, but it always passes even if no exception is thrown. What is wrong?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DebugTest {
    @Test
    void testException() {
        try {
            methodThatShouldThrow();
        } catch (IndexOutOfBoundsException e) {
            // expected
        }
    }

    void methodThatShouldThrow() {
        // no exception thrown here
    }
}
AThe test does not use assertThrows, so it passes even if no exception is thrown.
BThe catch block swallows the exception, causing the test to fail.
CThe method throws a different exception type, causing a test error.
DThe test method is missing the @Test annotation.
Attempts:
2 left
💡 Hint
Consider how JUnit knows a test should fail when an exception is missing.
framework
expert
2:00remaining
How does JUnit 5's assertThrows improve exception testing compared to older methods?
Which feature of assertThrows in JUnit 5 makes exception testing more reliable and readable than older try-catch based tests?
AIt catches all exceptions silently, so tests never fail due to exceptions.
BIt automatically fails the test if the expected exception is not thrown, reducing boilerplate code.
CIt requires manual verification of exception messages after catching exceptions.
DIt disables exception handling during test execution to speed up tests.
Attempts:
2 left
💡 Hint
Think about how assertThrows simplifies test code and failure detection.