I am writing unit tests for my application and I was wondering if it is possible for the Mockito framework to affect an object that is passed into a method that returns void of a mocked class. For instance, calling a mocked validation class that contains a method that returns void but tracks various changes and metadata via an object passed in as an argument. .
public GetCartItemsOutput getCartItems(GetCartItemsInput getCartItemsInput) {
CartItemsFilter cartItemsFilter = new CartItemsFilter();
validator.validateCartItemsInput(getCartItemsInput, cartItemsFilter); ...
I mocked the validator class for my other tests but for this one I need mock the changes to the cartItemsFilter object which I do not know how to do.
The answer is yes, you can, and there are basically two levels of doing this, based on the need of your test.
If you merely want to test the interaction with the mocked object, you can simply use the
verify()method, to verify that the void method was called.If your test genuinely needs the mocked object to modify parameters passed to it, you will need to implement an
Answer:EDITED to show proper form of using Answer with void method
In Java 8+, the above is simplified with a lambda: