Using Mockito, how do I test a \'finite loop\' ?
I have a method I want to test that looks like this:
public void dismissSearchAreaSuggestions()
{
As long as you make all of your stubs a part of the same chain, Mockito will proceed through them in sequence, always repeating the final call.
// returns false, false, true, true, true...
when(your.mockedCall(param))'
.thenReturn(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE);
You can also do so with this syntax...
// returns false, false, true, true, true...
when(your.mockedCall(param))
.thenReturn(Boolean.FALSE)
.thenReturn(Boolean.FALSE)
.thenReturn(Boolean.TRUE);
...which can come in handy if the actions aren't all return values.
// returns false, false, true, then throws an exception
when(your.mockedCall(param))
.thenReturn(Boolean.FALSE)
.thenReturn(Boolean.FALSE)
.thenReturn(Boolean.TRUE)
.thenThrow(new Exception("Called too many times!"));
If you want things to get more complex than that, consider writing an Answer.