Complete the code to assert that a NullPointerException is thrown.
assertThrows([1].class, () -> { throw new NullPointerException(); });
The assertThrows method expects the exception class that should be thrown. Here, NullPointerException.class is correct.
Complete the code to assert that an ArithmeticException is thrown when dividing by zero.
assertThrows([1].class, () -> { int result = 10 / 0; });
Dividing by zero throws an ArithmeticException, so that is the correct exception to assert.
Fix the error in the assertThrows usage to correctly check for IndexOutOfBoundsException.
assertThrows([1], () -> { List<String> list = List.of(); list.get(1); });
The assertThrows method requires the exception class literal, which is IndexOutOfBoundsException.class. Passing the class name without .class or an instance causes errors.
Fill both blanks to assert that a NumberFormatException is thrown when parsing an invalid number.
assertThrows([1].class, () -> { Integer.parseInt([2]); });
The code should assert NumberFormatException.class and parse the invalid string "abc" which causes the exception.
Fill all three blanks to assert that an IllegalArgumentException is thrown with a specific message.
IllegalArgumentException exception = assertThrows([1].class, () -> { throw new IllegalArgumentException([2]); }); assertEquals([3], exception.getMessage());
The test asserts that IllegalArgumentException.class is thrown with the message "Invalid input". The message is passed to the exception and checked with assertEquals.