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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.