Mockito系列-开篇

2020-12-01 18:04:24 浏览数 (1)

开启Mockito单元测试系列,这是第一篇。本文将介绍如何用Mockito来mock一个股票服务接口,在服务尚未实现的情况下,验证一个客户股票投资组合的计算逻辑。 谨以此文纪念2020年春美股的一周两次熔断

股票类

首先定义一个股票类

代码语言:javascript复制
package com.github.stock;

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

//节约篇幅,get/set省略
}

然后定义一个服务接口

代码语言:javascript复制
public interface StockService {
    public double getPrice(Stock stock);
}

实际案例中,我们可以通过调用行情服务商提供的接口来获得股票行情。假设目前这个服务尚未开发完成。 最后,我们定义一个客户的股票投资组合。

代码语言:javascript复制
package com.github.stock;

import java.util.List;

public class Portfolio {
    private StockService stockService;
    private List<Stock> stocks;

//省略get/set

    public double getMarketValue(){
        double marketValue = 0.0;

        for(Stock stock:stocks){
            marketValue  = stockService.getPrice(stock) * stock.getQuantity();
        }
        return marketValue;
    }
}

用Mockito来进行单元测试

在StockService尚未实现的情况下,如何对Protfolio类进行测试?

代码语言:javascript复制
package com.github.stock;

//import省略
public class PortfolioTest {
    Portfolio portfolio;
    StockService stockService;

    @BeforeEach
    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);
    }
    @Test
    public void testMarketValue(){

        //Creates a list of stocks to be added to the portfolio
        List<Stock> stocks = new ArrayList<Stock>();
        Stock teslaStock = new Stock("1","tesla", 10);
        Stock amazonStock = new Stock("2","amazon",100);

        stocks.add(teslaStock);
        stocks.add(amazonStock);

        //add stocks to the portfolio
        portfolio.setStocks(stocks);

        //mock the behavior of stock service to return the value of various stocks
        when(stockService.getPrice(teslaStock)).thenReturn(500.00);
        when(stockService.getPrice(amazonStock)).thenReturn(1000.00);

        assertThat(portfolio.getMarketValue()).isEqualTo(105000.0);
    }
}

为了测试portfolio.getMarketValue这个方法,

  • 用Mockito Mock了stockService
  • 用Mockito的when/thenReturn组合制造stub。当getPrice方法被调用时,我们返回一个指定的价格,如tesla的股票是500。
  • 设置客户的投资组合
  • 调用assertJ的assertThat断言来验证getMarketValue的结果是否符合预期。

image.png

>>>>我是可爱的小绿条<<<<

Maven依赖

为了能使用Mockito和AssertJ,需要在项目的pom.xml中添加如下依赖

代码语言:javascript复制
         <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.3.3</version>
            <scope>test</scope>
        </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>3.15.0</version>
      <scope>test</scope>
    </dependency>

0 人点赞