Normally with Mockito, if you're stubbing a method that gets called multiple times, you'd do
Mockito
.doReturn(0)
.doReturn(1)
.doReturn(2)
.when(mock).getValue();
I'd like to programaitcally stub a method that gets called multiple times, something like
Stubber stubber;
for (int i = 0; i < 8; ++i) {
stubber.doReturn(i);
}
stubber.when(mock).getValue();
My problem is there doesn't seem to be a public Stubber
factory method. There's org.mockito.internal.MockitoCore.stubber()
and new org.mockito.internal.stubbing.StubberImpl()
, but both are internal, and using them feels wrong.
Is there a better pattern for programatically stubbing like this? Is there a better way to get an empty Stubber
?
One solution would be when().thenReturn()
, but I've been avoiding that since reading on the difference between doReturn() and then().