mockito Mocking consecutive calls to a void return method

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any mockito Question?