Saturday, February 20, 2016

Testing method arguments using JUnit and Mockito ArgumentCaptor with example

Multiple times, we get to a situation, where we have a  public void method in our program, whose behaviour needs to be tested using  Junit.  Mostly such void methods, take some arguments to perform some operation locally and then pass objects for remote processing.

Automated testing of such scenarios can be done in one of following two approaches. - 
  1. Write a unit test and verify that our public method is performing necessary operations accurately and sending correct object to remote resources.
  2. Write an integration test by including third party module as well and then verify result from the remote resource.

In this article, we will be focus on first approach, where we write a JUnit test to ensure that our method is performing local operations accurately and sending the right object with right values to remote resource.

we can write such test cases using Mockito framework and its Argument captor API. Argument API provides us methods  like capture, getValue and getAllValues to capture the values passed by the method under test to mock of remote resource  and retrieve those values for verification.

In this Mockito Agrument captor example, we have a class MailProcessor, which takes the employee object from database on the basis of id, transform it to dto object  and pass it to mailing service for sending mail.

Technology used -
java version "1.7.0_06"
Apache Maven 3.0.4
junit -4.12
mockito -1.10.19

pom.xml -

<project xmlns="" xmlns:xsi=""

Project Structure

package salil.mockito.argumentcaptorexample;

public class Employee {

 private int employeeId;
 private String name;
 private String email;
 private int salarry;

 public int getEmployeeId() {
  return employeeId;

 public void setEmployeeId(int employeeId) {
  this.employeeId = employeeId;

 public String getName() {
  return name;

 public void setName(String name) { = name;

 public String getEmail() {
  return email;

 public void setEmail(String email) { = email;

 public int getSalarry() {
  return salarry;

 public void setSalarry(int salarry) {
  this.salarry = salarry;


package salil.mockito.argumentcaptorexample;

public interface EmployeeDao {

 Employee getEmployee(int employeeId);

package com.salil.argumentCaptor.domain;

public class EmpMailDTO {

 private final String reciepientEmail;
 private final String sender="";
 private final String subject="Salary notification";
 private final static String templateMessage="Dear %s\n\n we are processing your salary amount %s. \n\n Regards\nAdmin";
 private final String message;
 public EmpMailDTO(final String name,final String reciepientEmail,final int salary)
  this.message=String.format(templateMessage, name,salary);
 public String getReciepientEmail() {
  return reciepientEmail;

 public String getSender() {
  return sender;
 public String getMessage() {
  return message;

 public String getSubject() {
  return subject;

package com.salil.argumentCaptor;

import com.salil.argumentCaptor.domain.EmpMailDTO;

public interface MailingService {
 void sendEmail(EmpMailDTO employeeMail);

Method to be tested - MailProcessor.sendEmailToEmployee -
package salil.mockito.argumentcaptorexample;

public class MailProcessor {

 private final EmployeeDao employeeDao;
 private final MailingService mailingService;
 public MailProcessor(EmployeeDao employeeDao, MailingService mailer) {
  this.employeeDao = employeeDao;
  this.mailingService = mailer;
 public void sendEmailToEmployee(int employeeId)
  Employee employee = employeeDao.getEmployee(employeeId);
  EmpMailDTO empMailDTO = new EmpMailDTO(employee.getName(), employee.getEmail(), employee.getSalarry());
 private boolean isEmployeeMailValid(String email)
  return email!=null;

Corresponding test case would be as following -

package salil.mockito.argumentcaptorexample;

import static org.junit.Assert.*;

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

public class MailProcessorTest {

 private EmployeeDao employeeDao;
 private MailingService mailingService;
 private int employeeId=1;
 private String name="Salil";
 private String email="";
 private int salarry=5000;
 private Employee employee;
 public void setup() {
  employee = new Employee();
 public void testSendEmailToEmployee() {
  MailProcessor mailProcessor = new MailProcessor(employeeDao, mailingService);
//  Instantiate argument captor to capture the argument of EmployeeMail type  
  ArgumentCaptorargumentCaptor =ArgumentCaptor.forClass(EmpMailDTO.class);
//  Perform operation
//  Capture the argument
//  verify the correctness of the receiving object
  EmpMailDTO empMailDTO=argumentCaptor.getValue();
  assertEquals(empMailDTO.getMessage(),"Dear Salil\n\n we are processing your salary amount 5000. \n\n Regards\nAdmin");


This complete project can be accessed from the following git hub location.

Please let us know whether you found this article useful and your experience on using argument captor in your project.

No comments:

Post a Comment