How would you use Mockito to simulate the throwing of an exception by a method?

  • Use doThrow(Exception.class).when(mockedObject).methodCall()
  • Use expect(exception).when(mockedObject).methodCall()
  • Use mockedObject.throwException(Exception.class)
  • Use when(methodCall).thenThrow(Exception.class)
In Mockito, you can simulate the throwing of an exception by a method using doThrow(Exception.class).when(mockedObject).methodCall(). This sets up the behavior that when methodCall is invoked on mockedObject, it will throw the specified exception.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *