Complete the code to mock a method to throw an exception using when().thenThrow().
when(mockedList.[1](0)).thenThrow(new IndexOutOfBoundsException());
The get method is commonly mocked to throw exceptions like IndexOutOfBoundsException when accessing invalid indexes.
Complete the code to mock a service method to throw a custom exception.
when(userService.[1](anyString())).thenThrow(new UserNotFoundException());The method findUserById is typically used to retrieve a user and can throw UserNotFoundException if the user does not exist.
Fix the error in the code to correctly mock throwing an exception with when().thenThrow().
when(mockedMap.get([1])).thenThrow(new NullPointerException());The get method expects a key object. To pass a string key, it must be in quotes.
Fill both blanks to mock a method that throws an IllegalArgumentException when called with a negative number.
when(calculator.[1]([2])).thenThrow(new IllegalArgumentException());
The divide method can throw an exception when dividing by a negative number like -5.
Fill all three blanks to mock a repository method that throws SQLException when saving a null entity.
when(repository.[1]([2])).thenThrow(new [3]());
The save method throws SQLException when called with a null entity.