敲碎时间的人的个人专栏
上一篇

Mockito第一个demo

广告
选中文字可对指定文章内容进行评论啦,→和←可快速切换按钮,绿色背景文字可以点击查看评论额。
大纲

在深入了解 Mockito 框架的细节之前,让我们先看看一个正在运行的应用程序。在这个例子中,我们创建了一个股票服务的模拟来获取一些股票的虚拟价格,并对一个名为 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 类

让我们通过在其中注入 stockservice 的模拟来测试 Portfolio 类。 Mock 将由 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(){
      //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;
   }
}

第 5 步 - 验证结果

使用 javac 编译器编译类如下

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

现在运行 PortfolioTester 来查看结果

C:\Mockito_WORKSPACE>java PortfolioTester

验证输出

pass

 

版权声明:著作权归作者所有。

X

欢迎加群学习交流

联系我们