I have an interface that looks something like this.
public interface ParameterProvider
{
void provideParameter(Map<String, String> parameters);
}
An instance of it is used in the method that I want to write unit test for:
@Override
public void process(...)
{
...
Map<String, String> parameters = new HashMap<String, String>();
parameterProvider.provideParameter(parameters);
...
}
How can I mock ParameterProviderto perform actions on an argument passed to its provideParametermethod?
My first idea was to change the return type from voidto Mapand actually return the changed one Map(which in any case seems a lot nicer). Then testing is not a problem:
when(parameterProvider.provideParameter(anyMap())).thenReturn(MY_PARAMETERS);
However, since the interface is used in a structure that, for some odd reason, apparently requires the methods to be invalid, this is not an option now.
- . Mockito, , , .
:
doAnwser, , , :
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
args[0]
return null;
}})
.when(parameterProvider).provideParameter(anyMap());