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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>mockito</groupId>
 <artifactId>ArgumentCaptorExample</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.mockito</groupId>
   <artifactId>mockito-all</artifactId>
   <version>1.10.19</version>
  </dependency>
 </dependencies>
</project>

Project Structure


Employee.java

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) {
  this.name = name;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public int getSalarry() {
  return salarry;
 }

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

}

EmployeeDao.java

package salil.mockito.argumentcaptorexample;

public interface EmployeeDao {

 Employee getEmployee(int employeeId);
}


EmpMailDTO.java

package com.salil.argumentCaptor.domain;

public class EmpMailDTO {

 private final String reciepientEmail;
 private final String sender="admin@salilstock.blogspot.com";
 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.reciepientEmail=reciepientEmail;
  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;
 }
}

MailingService.java

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);
  if(isEmployeeMailValid(employee.getEmail()))
  {
  EmpMailDTO empMailDTO = new EmpMailDTO(employee.getName(), employee.getEmail(), employee.getSalarry());
  mailingService.sendEmail(empMailDTO);
  }
 }
 
 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;

@RunWith(MockitoJUnitRunner.class)
public class MailProcessorTest {

 @Mock
 private EmployeeDao employeeDao;
 @Mock
 private MailingService mailingService;
 
 private int employeeId=1;
 private String name="Salil";
 private String email="admin@salilstock.blogspot.com";
 private int salarry=5000;
 private Employee employee;
 
 @Before
 public void setup() {
  employee = new Employee();
  employee.setEmployeeId(employeeId);
  employee.setName(name);
  employee.setEmail(email);
  employee.setSalarry(salarry);
 }
 
 
 @Test
 public void testSendEmailToEmployee() {
  MailProcessor mailProcessor = new MailProcessor(employeeDao, mailingService);
  Mockito.when(employeeDao.getEmployee(1)).thenReturn(employee);
  
//  Instantiate argument captor to capture the argument of EmployeeMail type  
  ArgumentCaptorargumentCaptor =ArgumentCaptor.forClass(EmpMailDTO.class);
  
//  Perform operation
  mailProcessor.sendEmailToEmployee(1);
  
//  Capture the argument
  Mockito.verify(mailingService).sendEmail(argumentCaptor.capture());
  
//  verify the correctness of the receiving object
  EmpMailDTO empMailDTO=argumentCaptor.getValue();
  assertEquals(empMailDTO.getReciepientEmail(), this.email);
  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