Complete the code to assert that the method throws an IOException.
assertThrows([1].class, () -> methodThatThrows());
The method is expected to throw an IOException, so we assertThrows with IOException.class.
Complete the code to assert that the method throws a NullPointerException.
assertThrows([1].class, () -> methodThatThrowsNull());
The method throws a NullPointerException, so we assertThrows with NullPointerException.class.
Fix the error in the assertion to check for the correct exception type.
assertThrows([1].class, () -> methodThatThrows());
The method throws IOException, so the assertion must check for IOException.class, not a superclass or subclass.
Fill both blanks to assert that the method throws an IllegalArgumentException with the expected message.
IllegalArgumentException thrown = assertThrows([1].class, () -> methodThatThrowsIllegal()); assertTrue(thrown.getMessage().[2]("Invalid argument"));
We assertThrows with IllegalArgumentException.class and check that the message contains "Invalid argument" using contains().
Fill all three blanks to assert that the method throws a custom exception and check its cause and message.
CustomException ex = assertThrows([1].class, () -> methodThatThrowsCustom()); assertNotNull(ex.getCause()); assertEquals("[2]", ex.getCause().getClass().getSimpleName()); assertTrue(ex.getMessage().[3]("custom error"));
We assertThrows with CustomException.class, check that the cause is an IOException, and verify the message contains "custom error".