Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a non-null value that does not cause exception.
Using incorrect syntax for lambda expression.
✗ Incorrect
The method expects a null input to throw IllegalArgumentException.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue instead of assertEquals for message check.
Checking wrong exception message string.
✗ Incorrect
The exception message expected is "Invalid input".
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping expected and actual arguments in assertEquals.
Using null as expected message.
✗ Incorrect
The expected message should be the first argument in assertEquals.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception class.
Checking message with exact match instead of contains.
✗ Incorrect
The test checks that IllegalArgumentException is thrown and its message contains "Invalid".
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception class or cause type.
Checking message with contains instead of equals.
✗ Incorrect
The test asserts the exception type, exact message, and that the cause is a NullPointerException.