dimanche 6 décembre 2015

Mocking method with runnable arguments that return void

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?

Aucun commentaire:

Enregistrer un commentaire