I have a method of a mocked object that can be called multiple times (think recursion). The method is defined like this:
public void doCommit() { }
Reading Stubbing Consecutive Calls doco, something like this might do it:
when(mMockedObject.doCommit())
.thenThrow(new RuntimeException())
.thenCallRealMethod()
.thenThrow(new RuntimeException())
.thenCallRealMethod();
If you don't want to actually call the underlying method then you should use thenAnswer instead of thenCallRealMethod method and provide an empty stub imlementation.
I figured it out (with some hints from Igor). This is how you stub consecutive void method calls:
doThrow(new RuntimeException()).doNothing().doThrow(...).doNothing().when(mMockedObject).doCommit();
thanks Igor!