Drinking Mockito with A Simple Stock Broker Simulation Program

第三方库

junit-4.13.2

mockito-core-3.12.4

hamcrest-all-1.3

objenesis-1.2

byte-buddy-1.12.10

byte-buddy-agent-1.12.10

项目目录结构

src每个类的代码

Stock

java 复制代码
package com.yaya.mockito;

import java.math.BigDecimal;

public class Stock {
    private String symbol;
    private String name;
    private BigDecimal price;

    public Stock(String symbol, String name, BigDecimal price){
        this.symbol = symbol;
        this.name = name;
        this.price = price;
    }

    public String getSymbol(){
        return symbol;
    }

    public BigDecimal getPrice(){
        return price;
    }
}

MarketWatcher

java 复制代码
package com.yaya.junit;

import com.yaya.mockito.Stock;

public interface MarketWatcher {
    public Stock getQuote(String symbol);
}

Portfolio

java 复制代码
package com.yaya.junit;

import com.yaya.mockito.Stock;

import java.math.BigDecimal;
import java.util.Map;

public class Portfolio {

    private Map<String,String> stockMap;

    public void buy(Stock stock){

    }

    public BigDecimal getAvgPrice(Stock stock){
        return new BigDecimal("10.0");
    }

    public void sell(Stock stock,int shares){

    }

}

StockBroker

java 复制代码
package com.yaya.junit;

import com.yaya.mockito.Stock;

import java.math.BigDecimal;

public class StockBroker {

    private final static BigDecimal LIMIT = new BigDecimal("0.10");

    private final MarketWatcher market;


    public StockBroker(MarketWatcher market) {
        this.market = market;
    }

    public void perform(Portfolio portfolio, Stock stock){
        Stock liveStock = market.getQuote(stock.getSymbol());

        BigDecimal avgPrice = portfolio.getAvgPrice(stock);
        BigDecimal priceGained = liveStock.getPrice().subtract(avgPrice);
        BigDecimal percentGained = priceGained.divide(avgPrice);

        if(percentGained.compareTo(LIMIT)>0){
            portfolio.sell(stock,10);
        }else if(percentGained.compareTo(LIMIT)<0){
            portfolio.buy(stock);
        }
    }
}

test类代码

StockBrokerTest

java 复制代码
package com.yaya.junit;

import com.yaya.mockito.Stock;
import org.junit.Before;
import org.junit.Test;

import java.math.BigDecimal;

import static org.junit.Assert.assertNotNull;

import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

public class StockBrokerTest {

    MarketWatcher marketWatcher = mock(MarketWatcher.class);

    Portfolio portfolio = mock(Portfolio.class);

    StockBroker stockBroker;

    @Before
    public void setUp(){
        stockBroker = new StockBroker(marketWatcher);
    }

    @Test
    public void when_ten_percent_gain_then_the_stock_is_sold(){

        when(portfolio.getAvgPrice(isA(Stock.class))).thenReturn(new BigDecimal("10.00"));

        Stock myCorp = new Stock("YaYa","YaYa Corporation",new BigDecimal("11.20"));

        when(marketWatcher.getQuote(anyString())).thenReturn(myCorp);

        stockBroker.perform(portfolio,myCorp);

        verify(portfolio).sell(myCorp,10);
    }


}

运行结果

相关推荐
SkylerHu12 天前
nodejs运行的mock接口库mock-restful-api
后端·mock·nodejs·restful·mock-restful·mock-api
软件架构师笔记15 天前
深入浅出:Gin框架中的测试与Mock
go·mock·测试·gin
oscar99916 天前
透彻理解并解决Mockito模拟框架的单元测试无法运行的问题
单元测试·mockito
oscar99921 天前
Java 单元测试模拟框架-Mockito 的介绍
java·开发语言·单元测试·mockito
清尘沐歌1 个月前
前端开发快速进行 Mock 数据的方法
前端·mock
lianghyan1 个月前
Junit test with mock
junit·mockito·spy
又菜又爱玩的晴晴2 个月前
mockito+junit完成单元测试
junit·单元测试·mockito
AaronJonah2 个月前
Junit + Mockito保姆级集成测试实践
spring boot·junit·集成测试·mockito
Lossya2 个月前
【python实操】python小程序之UnitTest框架以及TestSuite书写方法
开发语言·python·小程序·继承·封装·unit testing
superpigwin2 个月前
go:极简上手使用 stretchr/testify 进行mock测试
go·mock