easymock快速入门
easymock是众多mock之中的很容易用的mock,今天刚开始学习,来个简单的教程.以购物车结算为例子,比如首先是每一个商品项的pojo。
public class Item {
private String name;
private int quantity;
public Item(String name, int quantity) {
super();
this.name = name;
this.quantity = quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public class ShoppingCart {
private String name;
private Store store = null;
private List<Item> items = new ArrayList();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public void addItem(Item item)
{
items.add(item);
}
public void setStore(Store store)
{
this.store=store;
}
public Store getStore()
{
return (this.store);
}
public Double calculateTotal()
{
Double total = 0.0;
for (Item item : this.items) {
total+= (store.getPrice(item.getName()) * item.getQuantity());
}
DecimalFormat decim = new DecimalFormat("0.00");
Double price = Double.parseDouble(decim.format(total));
return price;
}
在这个购物车的计算中,在计算总价格方面, total+= (store.getPrice(item.getName()) * item.getQuantity());这里,依赖了一个额外的对象store,根据store.getPrice()方法求出某个商品的单价, 但这里模拟的是现在根本不知道这个store 是如何实现的,有可能是第三方的,于是 easymock就派上用长了,它可以根据接口去模拟一个实现出来,下面直接看
ShoppingCartTest .java
public ShoppingCart cart = null;
public Store storeMock = null; @Before
public void initialize()
{
cart = new ShoppingCart();
storeMock = EasyMock.createMock(Store.class);
cart.setStore(storeMock);
} @Test
public void testShoppingCart()
{ EasyMock.expect(storeMock.getPrice("Mead Spiral Bound Notebook, College Rule")).andReturn(5.99);
EasyMock.expect(storeMock.getPrice("Kindle Fire HD 8.9")).andReturn(499.99); //开始使用mock
EasyMock.replay(storeMock); Item item1 = new Item("Mead Spiral Bound Notebook, College Rule", 3);
Item item2 = new Item("Kindle Fire HD 8.9",1); cart.addItem(item1);
cart.addItem(item2); double total = cart.calculateTotal(); System.out.println("Total price of items in shopping cart: $"+total);
assertEquals("Result",505.96, total,0);
} @After
public void cleanup()
{
cart=null;
storeMock=null;
}
junit一样,在before中,
@Before
public void initialize()
{
cart = new ShoppingCart();
storeMock = EasyMock.createMock(Store.class);
cart.setStore(storeMock);
}
storeMock = EasyMock.createMock(Store.class);就可以模拟一个实现出来了,
然后
EasyMock.expect(storeMock.getPrice("Mead Spiral Bound Notebook, College Rule")).andReturn(5.99); 这里,使用easymock的断言机制,断言出这个属的单价是5.99,然后记得使用 .EasyMock.replay(storeMock);就可以在真正的测试中,使用store这个对象了;最后记得cleanup中清理下.
easymock快速入门的更多相关文章
- Web Api 入门实战 (快速入门+工具使用+不依赖IIS)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...
- SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)
SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...
- 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)
今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...
- 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- Mybatis框架 的快速入门
MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...
- grunt快速入门
快速入门 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本 ...
- 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- 【第四篇】ASP.NET MVC快速入门之完整示例(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
随机推荐
- 自己理解BFC 和 stack context , stack order
1. stack order 发生在BFC计算好了之后. 2.一个一个的BFC里面,不同的block 里面的stack context 会根据 stack order的顺序,进行堆叠.呈现互相遮盖的效 ...
- [Algorithm] Powerset Problem
By given an array [1,2,3]: Return all possible combinations. for example: [[], [1], [2], [3], [1, 2] ...
- CareerCup Facebook Total number of substring palindrome
Write a function for retrieving the total number of substring palindromes. For example the input is ...
- CAD块参照转实体
经常,需要在CAD中插入块,比如图框,它的类型是INSERT,而不是Line和PolyLine.一般情况下,我们是不会去编辑它的,但有的时候需要选择它,比如在选择打印范围时,默认为过滤掉INSERT类 ...
- ubuntu12.04 lts 安装gcc 4.8
gcc 4.8.1 是第一个完全支持C++11 的编译器,Windows上可以安装mingw版的,在sourceforge 上有下载,安装也比较方便.在Linux上安装的话需要首先安装一些依赖库.在U ...
- Lintcode: Add Binary
C++ class Solution { public: /** * @param a a number * @param b a number * @return the result */ str ...
- IIS 之 IIS 7及以上多域名或端口绑定同一物理目录并设置不同默认文档
今天在 IIS 7 多端口或域名绑定同一物理目录,设置不同的默认文档遇到问题:同一物理目录的多个站点修改任意一个站点默认文档都会一起更改. 原因:在同一个物理目录下只有一个 web.config,并且 ...
- 微软BI 之SSRS 系列 - 不显示 Pie Chart 饼图上 0% 的数据
SSRS 小技巧系列专门用来记录 SSRS 报表开发过程中常用的小技巧 - 效果图 - 0% 的标签数据不需要显示出来. 效果图 - 正常的效果. 解决方法 - 使用 IIF 条件判断,如果计算值为 ...
- 使用gradle的application插件进行Spring-boot项目打包
1:在build.gradle中增加以下配置 fat jar并不总是一个合适的选择,比如需要依赖跟jar分离,使用gradle的application插件就可以做到. 在GradleTest项目中,b ...
- android形状drawable
1.在res目录下新建drawable目录. 2.新建一个xml文件. 3.採用drawable来定义资源. <? xml version="1.0" encoding=&q ...