Software Development

Mock methods that return void


Previously, I posted Void methods and Mockito in Java that verifies if any of the methods of a object passed as a argument were invoked.

This time we want to alter the behavior of a method that returns void.

Mock to alter a Method Behavior

Consider this code snippet:


How are we to mock this using Mockito? We want to test for a scenario where

Introducing Mockito.doAnswer

doAnswer allows us to change something within a method particularly objects passed as arguments.

When you inject the dog object to some object, it will be dealing with an unfriendly animal.



Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like