Sunday 21 July 2019

Mockito: Hello World example


Let me try to explain the usage of mocking with simple example.

EmployeeService.java: This class provides 'getEmployeeFirstNames' method, which takes list of employees as input and return the list of all the first names of employees.

EmployeeUtil.java: It uses the method 'getEmployeeFirstNames', of the class EmployeeService

Suppose let us assume, EmployeeUtil.java class implemented its functionality, by assuming EmployeeService will be implemented in time. Unfortunately, EmployeeService class is not yet implemented. Without mocking, you can't unit test the methods of EmployeeUtil class, since it is dependent on EmployeeService. By Mocking the methods of EmployeeService class we can test Employeeutil class.

To test the functionality of EmployeeUtil class, we should mock the method 'getEmployeeFirstNames' of class EmployeeService class.

Below statements define a mocked object if EmployeeService class, whenever, you call the getEmployeeFirstNames with employees, it returns employeeNames.

EmployeeService empService = Mockito.mock(EmployeeService.class);
PowerMockito.when(empService.getEmployeeFirstNames(employees)).thenReturn(employeeNames);

Find the below working application.

MethodNotImplementedException.java
package com.sample.exceptions;

public class MethodNotImplementedException extends RuntimeException {
 private static final long serialVersionUID = 1L;

 public MethodNotImplementedException() {
  super();
 }

 public MethodNotImplementedException(String message) {
  super(message);
 }

}


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();
 }

}


EmployeeService.java
package com.sample.service;

import java.util.List;

import com.sample.exceptions.MethodNotImplementedException;
import com.sample.model.Employee;

public class EmployeeService {

 public List<String> getEmployeeFirstNames(List<Employee> employees) {
  throw new MethodNotImplementedException();
 }
}


EmployeeUtil.java
package com.sample.util;

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

import com.sample.model.Employee;

import com.sample.service.EmployeeService;

public class EmployeeUtil {

 private EmployeeService empService;

 public EmployeeUtil(EmployeeService empService) {
  this.empService = empService;
 }

 /**
  * Return all employee names in upper case.
  * 
  * @param employees
  * @return
  */
 public List<String> getAllEmployeeFirstNamesInUpperCase(List<Employee> employees) {
  List<String> empNamesInLowerCase = empService.getEmployeeFirstNames(employees);
  List<String> result = new ArrayList<>(empNamesInLowerCase.size());

  for (String name : empNamesInLowerCase) {
   if (name == null) {
    continue;
   }

   result.add(name.toUpperCase());
  }

  return result;
 }

}


EmployeeUtilTest.java

import static org.junit.Assert.assertEquals;

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

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.util.EmployeeUtil;

public class EmployeeUtilTest {

 @Test
 public void getAllEmployeeFirstNamesInUpperCase_employees_namesMustBeInUpperCase() {

  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");

  List<Employee> employees = new ArrayList<>();
  List<String> employeeNames = new ArrayList<>();

  employees.add(emp1);
  employees.add(emp2);
  employees.add(emp3);
  employees.add(emp4);
  employees.add(emp5);

  employeeNames.add("Hari krishna");
  employeeNames.add("Kiran");
  employeeNames.add("Soumen");
  employeeNames.add("Sravya");
  employeeNames.add("Rachit");

  EmployeeService empService = Mockito.mock(EmployeeService.class);
  PowerMockito.when(empService.getEmployeeFirstNames(employees)).thenReturn(employeeNames);

  EmployeeUtil empUtil = new EmployeeUtil(empService);

  List<String> employeeFirstNames = empUtil.getAllEmployeeFirstNamesInUpperCase(employees);

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

 }
}



Previous                                                    Next                                                    Home

No comments:

Post a Comment