2

How to throw exception using PowerMock/PowerMockito/Mockito when a call to void method is made? Since the method is a void method so Mockito is not showing the option to throw exception. How can I go about it?

Siddharth
  • 2,046
  • 5
  • 26
  • 41
  • Possible duplicate of [How to mock a void static method to throw exception with Powermock?](http://stackoverflow.com/questions/7483628/how-to-mock-a-void-static-method-to-throw-exception-with-powermock) – Eric Apr 03 '17 at 18:29

1 Answers1

3

As explained in the documentation:

  1. Stubbing void methods with exceptions
doThrow(new RuntimeException()).when(mockedList).clear();

//following throws RuntimeException:
mockedList.clear();
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255