Mockito Example – Unit testing void methods using verify and ArgumentCaptor

This tutorial tries to explain Mockito’s verify and ArgumentCaptor features.

Other testing tutorials:
How to mock static, private methods (Powermock tutorial)
Unit testing void methods using verify and ArgumentCaptor
Mockito Deep Stubs Example
Selenium based automated test using Java, JUNIT and Maven – Example

When you need to unit test methods containing void methods which make calls to void methods of other classes, these features come in handy.

verify is used to verify if a method call is happened.

ArgumentCaptor is used along with verify to capture the arguments for the method call so that they can be asserted.

Code under test –

package com.idodevjobs;

public class ArgumentCaptorExample {
    ArgumentCaptorExampleDependent argumentCaptorExampleDependent;

    public void doSomething(String input) {
        ArgumentCaptorExampleRequest argumentCaptorExampleRequest = new ArgumentCaptorExampleRequest(input);

    public void setArgumentCaptorExampleDependent(ArgumentCaptorExampleDependent argumentCaptorExampleDependent) {
        this.argumentCaptorExampleDependent = argumentCaptorExampleDependent;

class ArgumentCaptorExampleDependent {
    public void helpDoSomething(ArgumentCaptorExampleRequest argumentCaptorExampleRequest) {

class ArgumentCaptorExampleRequest {
    private String input;

    public ArgumentCaptorExampleRequest(String input) {
        this.input = input;

    public String getInput() {
        return input;

    public void setInput(String input) {
        this.input = input;


package com.idodevjobs;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;

public class ArgumentCaptorExampleTest {

    private ArgumentCaptorExampleDependent argumentCaptorExampleDependent;

    private ArgumentCaptorExample argumentCaptorExample;

    public void setUp() throws Exception {
        argumentCaptorExample = new ArgumentCaptorExample();

    public void testDoSomething() throws Exception {
        ArgumentCaptor<ArgumentCaptorExampleRequest> argument = ArgumentCaptor.forClass(ArgumentCaptorExampleRequest.class);
        assertEquals("TEST_INPUT", argument.getValue().getInput());

In this example, the class under test ArgumentCaptorExample has a void method doSomething() that in turn makes a call to another void method helpDoSomething() in ArgumentCaptorExampleDependent class.

To test ArgumentCaptorExample’s doSomething() method, we’re creating mock for ArgumentCaptorExampleDependent class.

We’re using verify to test if the call to helpDoSomething() happens.

We’re using ArgumentCaptor to capture the arguments to helpDoSomething() method and
asserting the values.

Please leave a comment if you have any questions. Please share and comment if you find this useful.

This entry was posted in java and tagged , , , . Bookmark the permalink.

3 Responses to Mockito Example – Unit testing void methods using verify and ArgumentCaptor

  1. Pingback: Getting Started: Selenium (ChromeDriver) based UI automated test using Java, JUNIT and Maven – Example | iDoDevJobs

  2. Pingback: Mockito Deep Stubs Example | iDoDevJobs

  3. Pingback: Powermock – How to mock static, private methods (Powermock tutorial) | iDoDevJobs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s