PowerMock - Mock Final



In order to mock a final class or a final method, we need to first mock the complete class using following syntax.

Prepare Class

@PrepareForTest(StockUtil.class)

Mock Class

PowerMockito.mock(StockUtil.class);

Then we can mock the final methods.

PowerMockito.when(StockUtil.getPrice("1")).thenReturn(100.0);

Example

Below is the complete example of mocking static class.

File: Stock.java

package com.tutorialspoint;

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;
   }
}

File: StockService.java

package com.tutorialspoint;

public final class StockUtil {
   public final double getPrice(String stockId){
      //call stock service and return the price of the stock
      return 1;
   }
   public  StockUtil ()  {} 
}

File: Portfolio.java

package com.tutorialspoint;

import java.util.List;

public class Portfolio {
   private List<Stock> stocks;
   private StockUtil stockUtil;
   
   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 += stockUtil.getPrice(stock.getStockId()) * stock.getQuantity();
      }
      return marketValue;
   }
   
   public StockUtil getStockUtil() {
      return stockUtil;
   }

   public void setStockUtil(StockUtil stockUtil) {
      this.stockUtil = stockUtil;
   }
}

Let's test the Portfolio class, by mocking StockUtil. Mock will be created by PowerMock.

File: PortfolioTester.java

package com.tutorialspoint;
import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith( PowerMockRunner.class ) 
@PrepareForTest(  StockUtil.class  ) 
public class PortfolioTester {
   Portfolio portfolio;	

   @Before
   public void setUp(){
      //Create a portfolio object which is to be tested		
      portfolio = new Portfolio();		

      StockUtil stockUtil = PowerMockito.mock(StockUtil.class);
      portfolio.setStockUtil(stockUtil);	  
      PowerMockito.when(stockUtil.getPrice("1")).thenReturn(100.0);
      PowerMockito.when(stockUtil.getPrice("2")).thenReturn(300.0);
   }

   @Test
   public void 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);
      assertEquals ( 31000.0 ,  portfolio.getMarketValue(), 0.0001); 
   }
}

Output

Once you are done with creating the source files, you are ready for this step, which is compiling and running your program. To do this, keep PortfolioTester.Java file tab active, right click within the content area of PortfolioTester.Java and click on Run As > Junit Test option. If everything is fine with your application, this will run the test case in Eclipse JUnit Window and you can see the test case result as green means it is passed otherwise red being failed.

Advertisements