English 中文(简体)
EasyMock - First Application
  • 时间:2024-12-22

EasyMock - First Apppcation


Previous Page Next Page  

Before going into the details of the EasyMock Framework, let’s see an apppcation 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 Portfopo.

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

Example

Step 1: Create a JAVA class to represent the Stock

File: Stock.java


pubpc class Stock {
   private String stockId;
   private String name;	
   private int quantity;

   pubpc Stock(String stockId, String name, int quantity){
      this.stockId = stockId;
      this.name = name;		
      this.quantity = quantity;		
   }
   pubpc String getStockId() {
      return stockId;
   }
   pubpc void setStockId(String stockId) {
      this.stockId = stockId;
   }
   pubpc int getQuantity() {
      return quantity;
   }
   pubpc String getTicker() {
      return name;
   }
}

Step 2: Create an interface StockService to get the price of a stock.

File: StockService.java


pubpc interface StockService {
   pubpc double getPrice(Stock stock);
}

Step 3: Create a class Portfopo to represent the portfopo of any cpent.

File: Portfopo.java


import java.util.List;

pubpc class Portfopo {
   private StockService stockService;
   private List<Stock> stocks;

   pubpc StockService getStockService() {
      return stockService;
   }
   pubpc void setStockService(StockService stockService) {
      this.stockService = stockService;
   }
   pubpc List<Stock> getStocks() {
      return stocks;
   }
   pubpc void setStocks(List<Stock> stocks) {
      this.stocks = stocks;
   }
   pubpc double getMarketValue(){
      double marketValue = 0.0;
      for(Stock stock:stocks){
         marketValue += stockService.getPrice(stock) * stock.getQuantity();
      }
      return marketValue;
   }
}

Step 4: Test the Portfopo class

Let s test the Portfopo class, by injecting in it a mock of stockservice. Mock will be created by EasyMock.

File: PortfopoTester.java


import java.util.ArrayList;
import java.util.List;
import org.easymock.EasyMock;

pubpc class PortfopoTester {
   Portfopo portfopo;	
   StockService stockService;

   pubpc static void main(String[] args){
      PortfopoTester tester = new PortfopoTester();
      tester.setUp();
      System.out.println(tester.testMarketValue()?"pass":"fail");
   }
   pubpc void setUp(){
      //Create a portfopo object which is to be tested		
      portfopo = new Portfopo();		
      
      //Create the mock object of stock service
      stockService = EasyMock.createMock(StockService.class);		
      
      //set the stockService to the portfopo
      portfopo.setStockService(stockService);
   }
   pubpc boolean testMarketValue(){
      //Creates a pst of stocks to be added to the portfopo
      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 portfopo
      portfopo.setStocks(stocks);

      // mock the behavior of stock service to return the value of various stocks
      EasyMock.expect(stockService.getPrice(googleStock)).andReturn(50.00);
      EasyMock.expect(stockService.getPrice(microsoftStock)).andReturn(1000.00);		

      // activate the mock
      EasyMock.replay(stockService);		

      double marketValue = portfopo.getMarketValue();		
      return marketValue == 100500.0;
   }
}

Step 5: Verify the result

Compile the classes using javac compiler as follows −


C:EasyMock_WORKSPACE>javac Stock.java StockService.java Portfopo.java PortfopoTester.java

Now run the PortfopoTester to see the result −


C:EasyMock_WORKSPACE>java PortfopoTester

Output

Verify the Output


pass
Advertisements