easymock快速入门
来源:互联网 发布:剑三白发道长捏脸数据 编辑:程序博客网 时间:2024/05/18 01:53
easymock是众多mock之中的很容易用的mock,今天刚开始学习,来个简单的教程.以购物车结算为例子,比如首先是每一个商品项的pojo
然后是购物车的:
在这个购物车的计算中,在计算总价格方面,
total+= (store.getPrice(item.getName()) * item.getQuantity());
这里,依赖了一个额外的对象store,根据store.getPrice()方法求出某个商品的单价,
但这里模拟的是现在根本不知道这个store 是如何实现的,有可能是第三方的,于是
easymock就派上用长了,它可以根据接口去模拟一个实现出来,下面直接看
ShoppingCartTest .java
同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系列框架的大概原理就这样了,接下来就是深入的学习
- 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中清理下.
简单来说,mock系列框架的大概原理就这样了,接下来就是深入的学习
0 0
- easymock快速入门
- easymock快速入门
- easymock快速入门
- easymock快速入门
- EasyMock入门
- easyMock简单入门例子
- EasyMock实例入门总结
- easyMock简单入门例子 .
- EasyMock
- easyMock
- easymock
- EasyMock
- EasyMock
- Maven环境下easymock开发入门实例
- EasyMock一:EasyMock简介
- 快速入门
- EasyMock使用说明
- easymock学习
- ScrollView嵌套ListView或GridView时起始位置不是最顶部的解决办法
- (四)Spring核心思想 - IOC与DI
- DbUnit入门实战
- 如何写出优美的Java代码
- 学习易语言的个人心得
- easymock快速入门
- 北邮毕业十二年---摘自一个人
- UML基本架构建模--关联的通用建模技术(一)
- 分布式版本控制-Git(二)
- 上传图片(jpeg, png各种类型), 及视频
- 网线拔下来,本机SQLSERVER连不上的解决方法
- 塔式服务器、机架式服务器、刀片服务器区别小结
- Android中FTP服务器搭建、客户端编写
- ubuntu下给firefox安装flash插件