0
0
JUnittesting~10 mins

Checking exception message 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 check that the exception is thrown.

JUnit
assertThrows(IllegalArgumentException.class, () -> methodThatThrows([1]));
Drag options to blanks, or click blank then click option'
A5
B""
Cnull
Dnew Object()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a non-null value that does not cause exception.
Using incorrect syntax for lambda expression.
2fill in blank
medium

Complete the code to capture the exception and check its message.

JUnit
Exception exception = assertThrows(IllegalArgumentException.class, () -> methodThatThrows(null));
assertEquals([1], exception.getMessage());
Drag options to blanks, or click blank then click option'
A"Invalid input"
B"Exception occurred"
C"Error"
D"Null value"
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue instead of assertEquals for message check.
Checking wrong exception message string.
3fill in blank
hard

Fix the error in the assertion to correctly check the exception message.

JUnit
assertEquals([1], exception.getMessage());
Drag options to blanks, or click blank then click option'
A"Error"
Bexception.getMessage()
Cnull
D"Invalid input"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping expected and actual arguments in assertEquals.
Using null as expected message.
4fill in blank
hard

Fill both blanks to check exception type and message in one test.

JUnit
Throwable thrown = assertThrows([1].class, () -> methodThatThrows(null));
assertTrue(thrown.getMessage().contains([2]));
Drag options to blanks, or click blank then click option'
AIllegalArgumentException
B"Invalid"
C"Error"
DNullPointerException
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception class.
Checking message with exact match instead of contains.
5fill in blank
hard

Fill all three blanks to assert exception type, message, and cause.

JUnit
IllegalArgumentException ex = assertThrows([1].class, () -> methodThatThrows(null));
assertEquals([2], ex.getMessage());
assertNotNull(ex.getCause());
assertTrue(ex.getCause() instanceof [3]);
Drag options to blanks, or click blank then click option'
AIllegalArgumentException
B"Invalid input"
CNullPointerException
DRuntimeException
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception class or cause type.
Checking message with contains instead of equals.