easymock快速入门
easymock是众多mock之中的很容易用的mock,今天刚开始学习,来个简单的教程.以购物车结算为例子,比如首先是每一个商品项的pojo。
1 public class Item { 2 3 private String name; 4 private int quantity; 5 6 public Item(String name, int quantity) { 7 super(); 8 this.name = name; 9 this.quantity = quantity; 10 } 11 public String getName() { 12 return name; 13 } 14 public void setName(String name) { 15 this.name = name; 16 } 17 public int getQuantity() { 18 return quantity; 19 } 20 public void setQuantity(int quantity) { 21 this.quantity = quantity; 22 }
1 public class ShoppingCart { 2 3 private String name; 4 private Store store = null; 5 6 private List<Item> items = new ArrayList(); 7 8 public String getName() { 9 return name; 10 } 11 12 public void setName(String name) { 13 this.name = name; 14 } 15 16 public List<Item> getItems() { 17 return items; 18 } 19 20 public void setItems(List<Item> items) { 21 this.items = items; 22 } 23 24 25 public void addItem(Item item) 26 { 27 items.add(item); 28 } 29 30 31 public void setStore(Store store) 32 { 33 this.store=store; 34 } 35 36 public Store getStore() 37 { 38 return (this.store); 39 } 40 41 public Double calculateTotal() 42 { 43 Double total = 0.0; 44 for (Item item : this.items) { 45 total+= (store.getPrice(item.getName()) * item.getQuantity()); 46 } 47 48 DecimalFormat decim = new DecimalFormat("0.00"); 49 Double price = Double.parseDouble(decim.format(total)); 50 51 return price; 52 }
在这个购物车的计算中,在计算总价格方面, total+= (store.getPrice(item.getName()) * item.getQuantity());这里,依赖了一个额外的对象store,根据store.getPrice()方法求出某个商品的单价, 但这里模拟的是现在根本不知道这个store 是如何实现的,有可能是第三方的,于是 easymock就派上用长了,它可以根据接口去模拟一个实现出来,下面直接看
ShoppingCartTest .java
1 public ShoppingCart cart = null; 2 public Store storeMock = null; 3 4 @Before 5 public void initialize() 6 { 7 cart = new ShoppingCart(); 8 storeMock = EasyMock.createMock(Store.class); 9 cart.setStore(storeMock); 10 } 11 12 13 @Test 14 public void testShoppingCart() 15 { 16 17 18 EasyMock.expect(storeMock.getPrice("Mead Spiral Bound Notebook, College Rule")).andReturn(5.99); 19 EasyMock.expect(storeMock.getPrice("Kindle Fire HD 8.9")).andReturn(499.99); 20 21 //开始使用mock 22 EasyMock.replay(storeMock); 23 24 Item item1 = new Item("Mead Spiral Bound Notebook, College Rule", 3); 25 Item item2 = new Item("Kindle Fire HD 8.9",1); 26 27 cart.addItem(item1); 28 cart.addItem(item2); 29 30 double total = cart.calculateTotal(); 31 32 System.out.println("Total price of items in shopping cart: $"+total); 33 assertEquals("Result",505.96, total,0); 34 } 35 36 @After 37 public void cleanup() 38 { 39 cart=null; 40 storeMock=null; 41 }
junit一样,在before中,
1 @Before 2 public void initialize() 3 { 4 cart = new ShoppingCart(); 5 storeMock = EasyMock.createMock(Store.class); 6 cart.setStore(storeMock); 7 }
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中清理下.