博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
easymock快速入门
阅读量:6971 次
发布时间:2019-06-27

本文共 2818 字,大约阅读时间需要 9 分钟。

hot3.png

easymock是众多mock之中的很容易用的mock,今天刚开始学习,来个简单的教程.以购物车结算为例子,比如首先是每一个商品项的pojo
[code="java"]
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;
}
[/code]
  然后是购物车的:
[code="java"]
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;
}
[/code]
   在这个购物车的计算中,在计算总价格方面,
 total+= (store.getPrice(item.getName()) * item.getQuantity());
  这里,依赖了一个额外的对象store,根据store.getPrice()方法求出某个商品的单价,
但这里模拟的是现在根本不知道这个store 是如何实现的,有可能是第三方的,于是
easymock就派上用长了,它可以根据接口去模拟一个实现出来,下面直接看
ShoppingCartTest .java
    [code="java"]
public ShoppingCart cart = null;
public Store storeMock = null;
@Before
public void initialize()
{
cart = new ShoppingCart();
storeMock = EasyMock.createMock(Store.class);
cart.setStore(storeMock);
}
    
    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);
}
public void cleanup() 
{
cart=null;
storeMock=null;
}
    
[/code]
   同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中清理下.
     简单来说,mock系列框架的大概原理就这样了,接下来就是深入的学习
   

转载于:https://my.oschina.net/jackyrong/blog/161777

你可能感兴趣的文章
SSDB Windows安装包
查看>>
使用gulp-connect实现web服务器
查看>>
Android APP弱网测试问题和解决分析
查看>>
深入研究EF Core AddDbContext 引起的内存泄露的原因
查看>>
set集合
查看>>
今天不谈技术,说说一些常用的软件~By 逆天
查看>>
Python爬虫入门二之爬虫基础了解
查看>>
java入门概念个人理解之访问修饰符
查看>>
分布式 vs 集群 主从 vs 集群
查看>>
javascript数组操作汇总
查看>>
静态链表
查看>>
Ubuntu 12.04中文输入法的安装
查看>>
[转] 你真的了解回流和重绘吗
查看>>
[转] babel-present-env 与 babel-polyfill 学习总结
查看>>
openstack学习(一)kvm-libvirt
查看>>
使用pytesseract识别简单验证码
查看>>
1103 Integer Factorization
查看>>
Promise 简易实现 - 面试专用
查看>>
PHP —— 读取文件到二维数组
查看>>
Mysql中select的正确姿势
查看>>