Alex Rivera | Logout

Calling callbacks with Mockito

Asked 2012-11-28T23:54:46.347
78

I have some code

service.doAction(request, Callback<Response> callback);

How can I using Mockito grab the callback object, and call callback.reply(x)

Edit
Report

1 Answer

2
when(service.doAction(any(Request.class), any(Callback.class))).thenAnswer(
    new Answer() {
    Object answer(InvocationOnMock invocation) {
        Callback<Response> callback =
                     (Callback<Response>) invocation.getArguments()[1];
        callback.reply(/*response*/);
    }
});
answered 2012-11-29T00:00:21.977

Your Answer