Sunday, 21 July 2019

Mockito: Working with Mocksettings


By using MockSettings, we can create mock object with additional mock settings.

For example,
PrintService printServiceMock = PowerMockito.mock(PrintService.class,Mockito.withSettings().name("PrintServiceMock").verboseLogging());

Above statement creates a mock object by giving a name and enable verbose logging.

What can I do with MockSettings?
a.   You can name the mock objects.
b.   You can create mocks that implement multiple interfaces
c.    Enable logging, it is helpful while debugging
d.   Register listeners, listeners will be notified on method invocations of mock object.
e.   you can specify the instance to spy on
f.     You can specify default answers to interactions
g.   You can configure a mock to be serializable

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,
    Mockito.withSettings().name("PrintServiceMock").verboseLogging());
  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");

 }
}


Run TestDemo.java, you will get below output in console.

############ Logging method invocation #1 on mock/spy ########
PrintServiceMock.printName("Hari krishna");
   invoked: -> at com.sample.service.EmployeeService.getandPrintEmployeeFirstNames(EmployeeService.java:19)
   has returned: "null"

############ Logging method invocation #2 on mock/spy ########
PrintServiceMock.printName("Kiran");
   invoked: -> at com.sample.service.EmployeeService.getandPrintEmployeeFirstNames(EmployeeService.java:19)
   has returned: "null"

############ Logging method invocation #3 on mock/spy ########
PrintServiceMock.printName("Soumen");
   invoked: -> at com.sample.service.EmployeeService.getandPrintEmployeeFirstNames(EmployeeService.java:19)
   has returned: "null"

############ Logging method invocation #4 on mock/spy ########
PrintServiceMock.printName("Sravya");
   invoked: -> at com.sample.service.EmployeeService.getandPrintEmployeeFirstNames(EmployeeService.java:19)
   has returned: "null"

############ Logging method invocation #5 on mock/spy ########
PrintServiceMock.printName("Rachit");
   invoked: -> at com.sample.service.EmployeeService.getandPrintEmployeeFirstNames(EmployeeService.java:19)
   has returned: "null"

############ Logging method invocation #6 on mock/spy ########
PrintServiceMock.printName("Hari krishna");
   invoked: -> at com.sample.test.TestDemo.getandPrintEmployeeFirstNames_employees_printNameShouldBeCalled(TestDemo.java:54)
   has returned: "null"

############ Logging method invocation #7 on mock/spy ########
PrintServiceMock.printName("Kiran");
   invoked: -> at com.sample.test.TestDemo.getandPrintEmployeeFirstNames_employees_printNameShouldBeCalled(TestDemo.java:55)
   has returned: "null"

############ Logging method invocation #8 on mock/spy ########
PrintServiceMock.printName("Soumen");
   invoked: -> at com.sample.test.TestDemo.getandPrintEmployeeFirstNames_employees_printNameShouldBeCalled(TestDemo.java:56)
   has returned: "null"

############ Logging method invocation #9 on mock/spy ########
PrintServiceMock.printName("Sravya");
   invoked: -> at com.sample.test.TestDemo.getandPrintEmployeeFirstNames_employees_printNameShouldBeCalled(TestDemo.java:57)
   has returned: "null"

############ Logging method invocation #10 on mock/spy ########
PrintServiceMock.printName("Rachit");
   invoked: -> at com.sample.test.TestDemo.getandPrintEmployeeFirstNames_employees_printNameShouldBeCalled(TestDemo.java:58)
   has returned: "null"


Previous                                                    Next                                                    Home

No comments:

Post a Comment