jeudi 5 février 2015

Mock void methods, which change their argument

I have an interface, which looks something like this



public interface ParameterProvider
{
void provideParameter(Map<String, String> parameters);
}


An instance of it is used in the method I want to write a unit test for:



@Override
public void process(...)
{
...
Map<String, String> parameters = new HashMap<String, String>();
parameterProvider.provideParameter(parameters);
...
}


How can I mock the ParameterProvider to perform actions on the argument, that's passed to its provideParameter-method?


My first idea was to change the return type from void to Mapand actually return the modified Map (which seems a lot nicer anyway). Then testing is no problem:



when(parameterProvider.provideParameter(anyMap())).thenReturn(MY_PARAMETERS);


However since the interface is used in a framework, which for some odd reason seems to require the methods to be void, this is not an option right now.


Is there a way to still mock this thing. I'm using Mockito, but I don't need to stick with it, if there are other mocking frameworks to get the job done.


Aucun commentaire:

Enregistrer un commentaire