Mocking consecutive calls to a void return method
Introduction#
The Mockito docs have an excellent example of how to provide a sequence of answers for multiple calls to a mock. However, they don’t cover how to do that for a method that returns void, other than noting that stubbing void methods require using the do family of methods.
Remarks#
Remember, for non-void methods, the when(mock.method()).thenThrow().thenReturn()
version (see docs) is preferred because it is argument type-safe and more readable.
Faking a transient error
Imagine you’re testing code that makes a call to this interface, and you want to make sure your retry code is working.
public interface DataStore {
void save(Data data) throws IOException;
}
You could do something like this:
public void saveChanges_Retries_WhenDataStoreCallFails() {
DataStore dataStore = new DataStore();
Data data = new Data();
doThrow(IOException.class).doNothing().when(dataStore).save(data);
dataStore.save(data);
verify(dataStore, times(2)).save(data);
verifyDataWasSaved();
}