Calling callbacks with Mockito

前端 未结 4 1814
梦毁少年i
梦毁少年i 2020-11-28 07:36

I have some code

service.doAction(request, Callback callback);

How can I using Mockito grab the callback object, and call c

4条回答
  •  执笔经年
    2020-11-28 07:44

    You want to set up an Answer object that does that. Have a look at the Mockito documentation, at https://static.javadoc.io/org.mockito/mockito-core/2.8.47/org/mockito/Mockito.html#answer_stubs

    You might write something like

    when(mockService.doAction(any(Request.class), any(Callback.class))).thenAnswer(
        new Answer() {
            Object answer(InvocationOnMock invocation) {
                ((Callback) invocation.getArguments()[1]).reply(x);
                return null;
            }
    });
    
    
    

    (replacing x with whatever it ought to be, of course)

    提交回复
    热议问题