I need to be able to call a real method of spy object based on some condition. I.e. if condition is true then call real method otherwise do something else.
To be clear, I need to throw an exception on first call and call real method on a second call. Is it possible to achieve by using Mockito?
Object object = Mockito.spy(new Object());
// On the first call I need to throw an exception like this
Mockito.doThrow(RuntimeException.class).when(object).toString();
// On the second call I need to call a real method
Mockito.doCallRealMethod().when(object).toString();