Saturday, 1 July 2017

PowerMock, EasyMock : Partially mock an object

Sometimes, you want to test some(not all) methods of an object. In that case, you need to mock the object partially. You can mock the object partially by using ‘createPartialMock’ method of PowerMock class.

Following snippet is used to mock the method ‘getEmployeeFirstNames’ of the class EmployeeService.

EmployeeService empService = PowerMock.createPartialMock(EmployeeService.class, "getEmployeeFirstNames");
EasyMock.expect(empService.getEmployeeFirstNames(emps)).andReturn(empNames);

PowerMock.replayAll();

‘createPartialMock’ method takes the class and the methods to mock as arguments. By using EasyMock.expect, we can set the expectation on calling of the method.

Find the following working application.

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

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

 public int getId() {
  return id;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return lastName;
 }

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

}

MethodNotImplementedException.java
package com.sample.model;

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

 public MethodNotImplementedException() {
  super();
 }

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

}


EmployeeService.java
package com.sample.service;

import java.util.List;

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

public class EmployeeService {

 /**
  * Return all the first names of the employees.
  * 
  * @param employees
  * @return
  */
 public List<String> getEmployeeFirstNames(List<Employee> employees) {
  throw new MethodNotImplementedException();
 }

 /**
  * Sort the names in their natural ordering.
  * 
  * @param names
  * @return
  */
 public List<String> sortListOfEmployeeNames(List<String> names) {
  names.sort(null);
  return names;
 }

 public List<String> sortAndGetAllTheEmployeeNames(List<Employee> employees) {
  List<String> empNames = getEmployeeFirstNames(employees);
  return sortListOfEmployeeNames(empNames);
 }
}


EmployeeServiceTest.java
package com.sample.tests;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;

import org.easymock.EasyMock;
import org.junit.Test;
import org.powermock.api.easymock.PowerMock;

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

public class EmployeeServiceTest {

 /**
  * This method test the function sortListOfEmployeeNames.
  * 
  */
 @Test
 public void sortAndGetAllTheEmployeeNames_ListOfEmployees_ExpectInNaturalOrdering() {
  Employee emp1 = new Employee(1, "Hari Krishna", "Gurram");
  Employee emp2 = new Employee(2, "Chamundeswari", "Majety");
  Employee emp3 = new Employee(3, "Rama Krishna", "Gurram");
  Employee emp4 = new Employee(4, "Gopi", "Battu");

  List<Employee> emps = Arrays.asList(emp1, emp2, emp3, emp4);
  List<String> empNames = Arrays.asList("Hari Krishna", "Chamundeswari", "Rama Krishna", "Gopi");

  EmployeeService empService = PowerMock.createPartialMock(EmployeeService.class, "getEmployeeFirstNames");
  EasyMock.expect(empService.getEmployeeFirstNames(emps)).andReturn(empNames);

  PowerMock.replayAll();
  List<String> names = empService.sortAndGetAllTheEmployeeNames(emps);

  PowerMock.verifyAll();

  assertEquals(names.get(0), "Chamundeswari");
  assertEquals(names.get(1), "Gopi");
  assertEquals(names.get(2), "Hari Krishna");
  assertEquals(names.get(3), "Rama Krishna");

 }
}






Previous                                                 Next                                                 Home

No comments:

Post a Comment