I'm writing tests for a presenter in a model-view-presenter pattern. I've mocked the view and testing the presenter. What I'd like to do is perform some piece of code when a mocked method is being called in the view.
To elaborate, the view sends and "save clicked event" to the presenter, the presenter then tells the view to "save changes in the form" after which the view commits the changes from the UI components to the model. Then the presenter can proceed to do whatever it wants in the with the model. Since the view is now mocked, it will not make any modifications to the model. So what I'd like to accomplish is that when saveChangeInTheForm is called in the mock, I will simulate the view's behavior and change the model's content.
Can this be accomplished using Mockito? What I have now is...
Mockito.doNothing().when(view).saveChangeInTheForm();
.. and ideally I'd like to replace "doNothing" with doMyCustomThing
This can be done by implementing an Answer
. E.g.:
final Model model = ...;
Answer modelModifier = new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
model.setX(1);
model.setY(2);
model.activateZ();
return null;
};
Mockito.doAnswer(modelModifier).when(view).saveChangeInTheForm();
It seems like your looking for Mockitos doAnswer method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With