This is the interface of the method:
void startWatch(MethodToWatch methodName, UUID uniqueID, Runnable toRun);
This is the implementation:
public void startWatch(MethodToWatch methodName, UUID uniqueID, Runnable toRun) {
this.startWatch(methodName, uniqueID);
start();
toRun.run();
stop();
}
i would like to mock this method with something like this:
IPerformanceStopWatch mock = mock(IPerformanceStopWatch.class);
when(performanceStopWatchFactory.getStartWatch()).thenReturn(mock);
when(mock.startWatch(any(), any(), any())).thenAnswer(new Answer<void>() {
@Override
public void answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Runnable torun = (Callable)args[2];
torun.run();
return;
}
});
the problem is that when(...) cannot get a method with return value of void.
how can i moke this method without using spy?
Use doAnswer():
// It is supposed here that Mockito.doAnswer is statically imported
doAnswer(...).when(mock).startWatch(etc);
You can just reuse the answer you have, which is good.
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