0
0
JUnittesting~20 mins

Checking exception type hierarchy in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Hierarchy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
JUnit Exception Hierarchy: Which exception is caught?
Consider the following JUnit test method. Which exception type will be caught and asserted in this test?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class ExceptionTest {
    @Test
    void testExceptionHierarchy() {
        Exception thrown = assertThrows(Exception.class, () -> {
            throw new IllegalArgumentException("Invalid argument");
        });
        assertEquals(IllegalArgumentException.class, thrown.getClass());
    }
}
AThe test will pass because IllegalArgumentException is a subclass of Exception and is caught.
BThe test will fail because Exception.class does not catch IllegalArgumentException.
CThe test will fail because assertThrows only catches RuntimeException and its subclasses.
DThe test will pass but the caught exception type will be Exception, not IllegalArgumentException.
Attempts:
2 left
💡 Hint
Remember that IllegalArgumentException extends RuntimeException, which extends Exception.
assertion
intermediate
1:30remaining
Correct Assertion for Exception Subclass in JUnit
You want to assert that a method throws a NullPointerException using JUnit 5. Which assertion correctly verifies the exception type?
AassertThrows(RuntimeException.class, () -> method());
BassertThrows(Throwable.class, () -> method());
CassertThrows(NullPointerException.class, () -> method());
DassertThrows(Exception.class, () -> method());
Attempts:
2 left
💡 Hint
Use the most specific exception type to verify the exact exception thrown.
🔧 Debug
advanced
2:30remaining
Why does this JUnit test fail to catch the exception?
Examine the following JUnit test. Why does it fail even though the method throws an IOException?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class FileTest {
    @Test
    void testIOException() {
        assertThrows(FileNotFoundException.class, () -> {
            throw new IOException("File error");
        });
    }
}
AThe test fails because FileNotFoundException is a subclass of IOException, so it should catch IOException.
BThe test fails because assertThrows cannot catch checked exceptions.
CThe test passes because IOException is a checked exception.
DThe test fails because IOException is not a subclass of FileNotFoundException.
Attempts:
2 left
💡 Hint
Check the inheritance relationship between IOException and FileNotFoundException.
🧠 Conceptual
advanced
1:30remaining
Understanding Exception Type Matching in JUnit assertThrows
In JUnit 5, when using assertThrows(ExpectedException.class, executable), which of the following statements is true about the exception matching?
AassertThrows matches exceptions of ExpectedException or any subclass of it.
BassertThrows matches any exception that implements the Throwable interface.
CassertThrows only matches exceptions of the exact class ExpectedException, not subclasses.
DassertThrows matches exceptions only if they are checked exceptions.
Attempts:
2 left
💡 Hint
Think about inheritance and polymorphism in Java exceptions.
framework
expert
3:00remaining
JUnit 5 Exception Assertion with Multiple Exception Types
You want to write a JUnit 5 test that passes if either IllegalArgumentException or NullPointerException is thrown by a method. Which approach correctly achieves this?
AUse assertThrowsAnyOf(IllegalArgumentException.class, NullPointerException.class, () -> method()) from JUnit 5.9+.
BUse assertThrows(Throwable.class, () -> method()) and then assert the exception is either IllegalArgumentException or NullPointerException.
CUse assertThrows(IllegalArgumentException.class, () -> method()) and catch NullPointerException separately.
DUse assertThrows(Exception.class, () -> method()) and check the exception type inside the test.
Attempts:
2 left
💡 Hint
Check if JUnit 5 has a built-in method to assert multiple exception types.