Sunday 21 July 2019

Mockito: check void instance methods


Sometimes, while testing a function, you would like to check whether a void function is called inside it or not.

You can check void instance methods by using 'Mockito.verify' method.

For example,
Mockito.verify(printServiceMock, Mockito.times(1)).printName("Hari krishna");

Above statement checks, whether printName method of printServiceMock object is called with argument ‘Hari krishna’ or not.

Find the below working application.

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 private double salary;
 private String country;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public double getSalary() {
  return salary;
 }

 public void setSalary(double salary) {
  this.salary = salary;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 public Employee(int id, String firstName, String lastName, double salary, String country) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
  this.salary = salary;
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
    .append(lastName).append(", salary=").append(salary).append(", country=").append(country).append("]");
  return builder.toString();
 }

}


PrintService.java
package com.sample.service;

public class PrintService {

 public void printName(String name){
  System.out.println(name);
 }
}


EmployeeService.java
package com.sample.service;

import java.util.ArrayList;
import java.util.List;

import com.sample.model.Employee;

public class EmployeeService {
 private PrintService printService;

 public EmployeeService(PrintService printService) {
  this.printService = printService;
 }

 public List<String> getandPrintEmployeeFirstNames(List<Employee> employees) {
  List<String> empNames = new ArrayList<>();

  for (Employee emp : employees) {
   printService.printName(emp.getFirstName());
   empNames.add(emp.getFirstName());
  }

  return empNames;

 }
}


TestDemo.java

package com.sample.test;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;

import com.sample.model.Employee;
import com.sample.service.EmployeeService;
import com.sample.service.PrintService;

public class TestDemo {
 private List<Employee> employees;

 @Before
 public void populateEmployees() {
  Employee emp1 = new Employee(1, "Hari krishna", "Gurram", 12345, "India");
  Employee emp2 = new Employee(2, "Kiran", "Kumnoor", 234556, "Germany");
  Employee emp3 = new Employee(3, "Soumen", "Mondle", 56789, "India");
  Employee emp4 = new Employee(4, "Sravya", "Guruju", 567890, "Japan");
  Employee emp5 = new Employee(5, "Rachit", "Kumar", 123645, "India");

  employees = new ArrayList<>();
  employees.add(emp1);
  employees.add(emp2);
  employees.add(emp3);
  employees.add(emp4);
  employees.add(emp5);

 }

 @Test
 public void getandPrintEmployeeFirstNames_employees_printNameShouldBeCalled() {
  /* Create mock object */
  PrintService printServiceMock = PowerMockito.mock(PrintService.class);
  EmployeeService empService = new EmployeeService(printServiceMock);

  List<String> employeeFirstNames = empService.getandPrintEmployeeFirstNames(employees);

  assertEquals(employeeFirstNames.size(), 5);
  assertEquals(employeeFirstNames.get(0), "Hari krishna");
  assertEquals(employeeFirstNames.get(1), "Kiran");
  assertEquals(employeeFirstNames.get(2), "Soumen");
  assertEquals(employeeFirstNames.get(3), "Sravya");
  assertEquals(employeeFirstNames.get(4), "Rachit");

  /* Verify whether printName method is called or not */
  Mockito.verify(printServiceMock, Mockito.times(1)).printName("Hari krishna");
  Mockito.verify(printServiceMock, Mockito.times(1)).printName("Kiran");
  Mockito.verify(printServiceMock, Mockito.times(1)).printName("Soumen");
  Mockito.verify(printServiceMock, Mockito.times(1)).printName("Sravya");
  Mockito.verify(printServiceMock, Mockito.times(1)).printName("Rachit");

 }
}





Previous                                                    Next                                                    Home

No comments:

Post a Comment