Sunday, October 18, 2015

Mockito Spy

Org.mockito.Spy
Now here comes the situation, the Employee class has one method, and when that method will be called not mockingly but really and call will return the value, we have to use that value to test our real method.

Did you get anything out of my above sentence, if no then read below:
Our Employee.getSummary() method requires address, which can’t be mocked or it requires to call real getTotalMarks() method of Employee class to supply marks during testing.
So what do you want here mocking 1 method, while calling come real method- it means you want to do partial mocking. We call this partial mocking spying as well.
Here is a basic example:
Employee.java

/**
 *
 */
package com.sushil.mockito;

/**
 * @author Sushil
 *
 */
public class Employee {
     String name;
     int  totalMarks;
     String address;
    
     public String getName() {
              return name;
     }
     public void setName(String name) {
              this.name = name;
     }
     public int getTotalMarks() {
              System.out.println("get total marks called");
              return totalMarks;
     }
     public void setTotalMarks(int totalMarks) {
              this.totalMarks = totalMarks;
     }
     public String getAddress() {
              return address;
     }
     public void setAddress(String address) {
              this.address = address;
     }
    
     public String getSummary(String name, int marks, String address){
              System.out.println(name + " from " + address + " has recieved " + marks + " marks. " );
              return "hello I am from real class";
     }
     @Override
     public String toString() {
              return "Employee [name=" + name + ", totalMarks=" + totalMarks
                                  + ", address=" + address + "]";
     }
    
}



Test Class:
     EmployeeTest.java


/**
 *
 */
package com.sushil.mockito;

import static org.junit.Assert.fail;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

/**
 * @author Sushil
 *
 */
@RunWith(MockitoJUnitRunner.class)
public class EmployeeTest {

     @Mock
     Employee emp, spy;
    
     @Before
     public void setUp() throws Exception {
              MockitoAnnotations.initMocks(emp);
              Employee emp1=new Employee();
              emp1.setAddress("H");
              emp1.setName("K");
              emp1.setTotalMarks(20);
              spy=spy(emp1);
              int marks=spy.getTotalMarks();
       doReturn("HELLO").when(spy).getSummary(anyString(),eq(marks), anyString());
     }

     /**
      * @throws java.lang.Exception
      */
     @After
     public void tearDown() throws Exception {
     }

     @Test
     public void test() {
                  System.out.println(spy.getSummary(“Output - 1”));
                  System.out.println(spy.getSummary("L",10,"Y"));
                  System.out.println(spy.getSummary(“Output - 2”));
               System.out.println(spy.getSummary("L",20,"Y"));
     }

}


OUTPUT:


 //the real get()  method of EMPLOYEE class is called due to spy.getTotalMarks()
get total marks called


//as we have not mocked method for marks equal to 10, it calls real getSummary() method.
Output – 1  
L from Y has received 10 marks.
hello I am from real class

//as we have mocked method for marks equal to 20, it returns the result mocked.
Output - 2
HELLO

No comments: