0
0
JUnittesting~10 mins

Checking exception type hierarchy in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assert that the method throws an IOException.

JUnit
assertThrows([1].class, () -> methodThatThrows());
Drag options to blanks, or click blank then click option'
ARuntimeException
BError
CException
DIOException
Attempts:
3 left
💡 Hint
Common Mistakes
Using a superclass like Exception instead of the specific IOException.
Using an unrelated exception type like RuntimeException.
2fill in blank
medium

Complete the code to assert that the method throws a NullPointerException.

JUnit
assertThrows([1].class, () -> methodThatThrowsNull());
Drag options to blanks, or click blank then click option'
ANullPointerException
BIOException
CIllegalArgumentException
DIndexOutOfBoundsException
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing NullPointerException with IllegalArgumentException.
Using a superclass Exception instead of the specific exception.
3fill in blank
hard

Fix the error in the assertion to check for the correct exception type.

JUnit
assertThrows([1].class, () -> methodThatThrows());
Drag options to blanks, or click blank then click option'
AIOException
BException
CFileNotFoundException
DRuntimeException
Attempts:
3 left
💡 Hint
Common Mistakes
Using a superclass Exception which is too general.
Using a subclass FileNotFoundException which may not be thrown.
4fill in blank
hard

Fill both blanks to assert that the method throws an IllegalArgumentException with the expected message.

JUnit
IllegalArgumentException thrown = assertThrows([1].class, () -> methodThatThrowsIllegal());
assertTrue(thrown.getMessage().[2]("Invalid argument"));
Drag options to blanks, or click blank then click option'
AIllegalArgumentException
Bcontains
CstartsWith
DendsWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong exception class like RuntimeException.
Using startsWith or endsWith which may fail if message format changes.
5fill in blank
hard

Fill all three blanks to assert that the method throws a custom exception and check its cause and message.

JUnit
CustomException ex = assertThrows([1].class, () -> methodThatThrowsCustom());
assertNotNull(ex.getCause());
assertEquals("[2]", ex.getCause().getClass().getSimpleName());
assertTrue(ex.getMessage().[3]("custom error"));
Drag options to blanks, or click blank then click option'
ACustomException
BIOException
Ccontains
DstartsWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception class in assertThrows.
Checking cause with incorrect class name.
Using startsWith instead of contains for message check.