(译) 04-Mockito-第一个应用程序

在深入了解 Mockito 框架的详细信息之前, 让我们来看看一个案例。在这个例子中, 我们创建了一个模拟的股票服务, 以获得一些股票的虚拟价格和单元测试的 java 类Portfolio

下面将以循序渐进的方式讨论此过程。

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

文件: Stock.java

/**
 * 股票信息类
 */
public class Stock {
    //股票ID
    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 {
    /**
     * 获取股票价格
     * @param stock
     * @return
     */
    public double getPrice(Stock stock);
}

步骤3−创建类组合以表示任何客户端的组合

文件: Portfolio. java

import java.util.List;

/**
 * 投资组合
 */
public class Portfolio {
    private StockService stockService;
    private List stocks;

    public StockService getStockService() {
        return stockService;
    }

    public void setStockService(StockService stockService) {
        this.stockService = stockService;
    }

    public List getStocks() {
        return stocks;
    }

    public void setStocks(List 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 的Mock。Mock将由 Mockito 创建。

文件: PortfolioTester. java

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 stocks = new ArrayList();
        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

翻译自 :https://www.tutorialspoint.com/mockito/mockito_first_application.htm

你可能感兴趣的:(Mockito,Mockito)