Mockito: Verifying that a void method has been called

Learn
1 min readSep 7, 2024

--

I have a method that is calling multiple dependent methods which take care of various disparate sub-tasks, and I needed to make sure that each of these sub-tasks are done. And I am using Mockito to write my tests.

So I had a mock for these dependency beans, and I was ensuring that each of these get invoked using the following syntax

verify(cashier).billCustomer(customer);
verify(transport).shipGoods(goods);

Then I came across a method that returns void, and Mockito wouldn’t let me call a void method in the same manner. I don’t like it. It has nothing to do with my intentionality, I want to validate that a void returning method has been invoked, just as much as I would have wanted to verify that an object returning method was invoked.

What I had to do was as below

verify(restaurant, times(1)).serveFood(customer);

Here times is about the number of times the particular method has been called.

--

--