Mockito Tutorial on Mockito First Application

before going into the details of the mockito framework, let's see an application in action. in this example, we've created a mock of stock service to get the dummy price of some stocks and unit tested a java class named portfolio.

the process is discussed below in a step-by-step manner.

step 1 − create a java class to represent the stock

file: stock.java

public class stock {
   private string stockid;
   private string name;	
   private int quantity;

   public stock(string stockid, string name, int quantity){
      this.stockid = stockid;
      this.name = name;		
      this.quantity = quantity;		
   }

   public string getstockid() {
      return stockid;
   }

   public void setstockid(string stockid) {
      this.stockid = stockid;
   }

   public int getquantity() {
      return quantity;
   }

   public string getticker() {
      return name;
   }
}

step 2 − create an interface stockservice to get the price of a stock

file: stockservice.java

public interface stockservice {
   public double getprice(stock stock);
}

step 3 − create a class portfolio to represent the portfolio of any client

file: portfolio.java

import java.util.list;

public class portfolio {
   private stockservice stockservice;
   private list<stock> stocks;

   public stockservice getstockservice() {
      return stockservice;
   }
   
   public void setstockservice(stockservice stockservice) {
      this.stockservice = stockservice;
   }

   public list<stock> getstocks() {
      return stocks;
   }

   public void setstocks(list<stock> stocks) {
      this.stocks = stocks;
   }

   public double getmarketvalue(){
      double marketvalue = 0.0;
      
      for(stock stock:stocks){
         marketvalue += stockservice.getprice(stock) * stock.getquantity();
      }
      return marketvalue;
   }
}

step 4 − test the portfolio class

let's test the portfolio class, by injecting in it a mock of stockservice. mock will be created by mockito.

file: portfoliotester.java

package com.tutorialspoint.mock;

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

import static org.mockito.mockito.*;

public class portfoliotester {
	
   portfolio portfolio;	
   stockservice stockservice;
	   
   
   public static void main(string[] args){
      portfoliotester tester = new portfoliotester();
      tester.setup();
      system.out.println(tester.testmarketvalue()?"pass":"fail");
   }
   
   public void setup(){
      //create a portfolio object which is to be tested		
      portfolio = new portfolio();		
  
      //create the mock object of stock service
      stockservice = mock(stockservice.class);		
  
      //set the stockservice to the portfolio
      portfolio.setstockservice(stockservice);
   }
   
   public boolean testmarketvalue(){
    	   
      //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);

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

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

      double marketvalue = portfolio.getmarketvalue();		
      return marketvalue == 100500.0;
   }
}

step 5 − verify the result

compile the classes using javac compiler as follows −

c:\mockito_workspace>javac stock.java stockservice.java portfolio.java portfoliotester.java

now run the portfoliotester to see the result −

c:\mockito_workspace>java portfoliotester

verify the output

pass