Mockito Tutorial on Mockito Overview

what is mocking?

mocking is a way to test the functionality of a class in isolation. mocking does not require a database connection or properties file read or file server read to test a functionality. mock objects do the mocking of the real service. a mock object returns a dummy data corresponding to some dummy input passed to it.

mockito

mockito facilitates creating mock objects seamlessly. it uses java reflection in order to create mock objects for a given interface. mock objects are nothing but proxy for actual implementations.

consider a case of stock service which returns the price details of a stock. during development, the actual stock service cannot be used to get real-time data. so we need a dummy implementation of the stock service. mockito can do the same very easily, as its name suggests.

benefits of mockito

  • no handwriting − no need to write mock objects on your own.

  • refactoring safe − renaming interface method names or reordering parameters will not break the test code as mocks are created at runtime.

  • return value support − supports return values.

  • exception support − supports exceptions.

  • order check support − supports check on order of method calls.

  • annotation support − supports creating mocks using annotation.

consider the following code snippet.

package com.tutorialspoint.mock;

import java.util.arraylist;
import java.util.list;

import static org.mockito.mockito.*;

public class portfoliotester {
   public static void main(string[] args){

      //create a portfolio object which is to be tested		
      portfolio portfolio = new portfolio();

      //creates a list of stocks to be added to the portfolio
      list<stock> stocks = new arraylist<stock>();
      stock googlestock = new stock("1","google", 10);
      stock microsoftstock = new stock("2","microsoft",100);

      stocks.add(googlestock);
      stocks.add(microsoftstock);		

      //create the mock object of stock service
      stockservice stockservicemock = mock(stockservice.class);

      // mock the behavior of stock service to return the value of various stocks
      when(stockservicemock.getprice(googlestock)).thenreturn(50.00);
      when(stockservicemock.getprice(microsoftstock)).thenreturn(1000.00);

      //add stocks to the portfolio
      portfolio.setstocks(stocks);

      //set the stockservice to the portfolio
      portfolio.setstockservice(stockservicemock);

      double marketvalue = portfolio.getmarketvalue();

      //verify the market value to be 
      //10*50.00 + 100* 1000.00 = 500.00 + 100000.00 = 100500
      system.out.println("market value of the portfolio: "+ marketvalue);
   }
}

let's understand the important concepts of the above program. the complete code is available in the chapter first application.

  • portfolio − an object to carry a list of stocks and to get the market value computed using stock prices and stock quantity.

  • stock − an object to carry the details of a stock such as its id, name, quantity, etc.

  • stockservice − a stock service returns the current price of a stock.

  • mock(...) − mockito created a mock of stock service.

  • when(...).thenreturn(...) − mock implementation of getprice method of stockservice interface. for googlestock, return 50.00 as price.

  • portfolio.setstocks(...) − the portfolio now contains a list of two stocks.

  • portfolio.setstockservice(...) − assigns the stockservice mock object to the portfolio.

  • portfolio.getmarketvalue() − the portfolio returns the market value based on its stocks using the mock stock service.