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


}

运行结果

相关推荐
又菜又爱玩的晴晴7 天前
mockito+junit完成单元测试
junit·单元测试·mockito
AaronJonah12 天前
Junit + Mockito保姆级集成测试实践
spring boot·junit·集成测试·mockito
Lossya16 天前
【python实操】python小程序之UnitTest框架以及TestSuite书写方法
开发语言·python·小程序·继承·封装·unit testing
superpigwin18 天前
go:极简上手使用 stretchr/testify 进行mock测试
go·mock
smacricket0021 天前
前端开发实践与后端开发解耦(一)-- 接口数据mock和接口字段映射
前端·javascript·vue.js·vue·mock·开发实践
金斗潼关2 个月前
使用Mockito进行单元测试
单元测试·白盒测试·mockito
Maer092 个月前
Typescript 使用 Jest 进行单元测试
javascript·typescript·unit testing
Mr.朱鹏3 个月前
MockIto插桩常规单元测试
java·spring·java-ee·kafka·单元测试·springboot·mockito
Java搬码工3 个月前
Mockito基础入门
mockito
言午夏4 个月前
junit mockito Base基类
junit·mockito