Testing a Callback
For testing a callback we'll look at two approaches: first, an ArgumentCaptor, and secondly the simple doAnswer() function.
ArgumentCaptor
Let's have a look at how we can use Mockito to capture the Callback object using an ArgumentCaptor:
@Test
public void givenServiceWithValidResponse_whenCallbackReceived_thenProcessed() {
ActionHandler handler = new ActionHandler(service);
handler.doAction();
verify(service).doAction(anyString(), callbackCaptor.capture());
Callback<Response> callback = callbackCaptor.getValue();
Response response = new Response();
callback.reply(response);
String expectedMessage = "Successful data response";
Data data = response.getData();
assertEquals(
"Successful message: ",
expectedMessage, data.getMessage());
}

You can also try this code with Online Java Compiler
Run Code
In this example, we first establish an ActionHandler before invoking its doAction() function. This is essentially a wrapper for our Simple Service doAction() method call, which is where our callback is invoked.
Following that, we validate that doAction() was called on our fake service instance, sending anyString() as the first parameter and callbackCaptor.capture() as the second, where we capture the Callback object. The captured value of the argument may then be returned using the getValue() function.
Now that we have the Callback object, we build a Response object that is valid by default before calling the respond function directly and ensuring that the response data has the right value.
doAnswer() Method
Now we'll look at a typical technique for stubging methods with callbacks, which involves utilizing Mockito's Answer object and doAnswer() function to stub the void method doAction:
@Test
public void givenServiceWithInvalidResponse_whenCallbackReceived_thenNotProcessed() {
Response response = new Response();
response.setIsValid(false);
doAnswer((Answer<Void>) invocation -> {
Callback<Response> callback = invocation.getArgument(1);
callback.reply(response);
Data data = response.getData();
assertNull("No data in invalid response: ", data);
return null;
}).when(service)
.doAction(anyString(), any(Callback.class));
ActionHandler handler = new ActionHandler(service);
handler.doAction();
}
We first build an incorrect Response object, which will later be utilized in the test. Then, on our fake service, we configure the Answer such that when doAction() is called, we intercept the invocation and grab the method arguments through invocation. To obtain the Callback argument, use getArgument(1). The final step is to establish the ActionHandler and call doAction, which invokes the Answer.

You can also try this code with Online Java Compiler
Run Code
Frequently Asked Questions
-
What is Mockito?
Mockito is a JAVA-based mocking framework that is used for unit testing of JAVA applications.
-
What is the Callback?
A callback is a piece of code supplied as an argument to a method, which is intended to call back (execute) the argument at a specified time.
-
What are two methods of testing a callback in Mockito?
ArgumentCaptor and doAnswer() Methord are the two methods of testing a callback in Mockito
-
In which testing method did we call ActionHandler before doAction() function?
In the ArgumentCaptor Methord.we call ActionHandler before doAction() function.
-
In which testing method did we call ActionHandler after doAction() function?
In the doAnswer() Method we call ActionaHandler after doAction() method.
Conclusion
In this article, we covered two different ways to approach testing callbacks when testing with Mockito. We hope that this blog has helped you enhance your knowledge regarding Testing Callbacks and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!