Mockito - 第一个应用程序

在深入了解 Mockito 框架的细节之前,让我们先看一个实际的应用程序。在这个例子中,我们创建了一个 Stock Service 模拟来获取一些股票的虚拟价格,并对名为 Portfolio 的 Java 类进行了单元测试。

下面将逐步讨论该过程。

步骤 1 − 创建一个 JAVA 类来表示股票

文件: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;
   }
}

步骤 2 − 创建一个接口 StockService 来获取股票价格

文件:StockService.java

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

步骤 3 − 创建一个 Portfolio 类来表示任何客户的投资组合

文件: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;
   }
}

步骤 4 − 测试 Portfolio 类

让我们通过向 Portfolio 类中注入 stockservice 的模拟来测试该类。模拟将由 Mockito 创建。

文件: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(){
        //创建要测试的投资组合对象
        portfolio = new Portfolio();
        
        //创建股票服务的模拟对象
        stockService = mock(StockService.class);
        
        //将 stockService 设置为投资组合
        portfolio.setStockService(stockService);
   }
   
   public boolean testMarketValue(){
    	   
        //创建要添加到投资组合的股票列表
        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);
        
        //将股票添加到投资组合
        portfolio.setStocks(stocks);
        
        //模拟股票服务的行为以返回各种股票的价值
        when(stockService.getPrice(googleStock)).thenReturn(50.00);
        when(stockService.getPrice(microsoftStock)).thenReturn(1000.00);
        
        double marketValue = portfolio.getMarketValue();
        r​​eturn marketValue == 100500.0;
   }
}

步骤 5 − 验证结果

使用 javac 编译器编译类,如下所示 −

C:\Mockito_WORKSPACE>javac Stock.java StockService.java Portfolio.java PortfolioTester.java

现在运行 PortfolioTester 以查看结果 −

C:\Mockito_WORKSPACE>java PortfolioTester

验证输出

pass