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的断言机制,断言出这个属的单价是5.99,然后记得使用 EasyMock.replay(storeMock);就可以在真正的测试中,使用store这个对象了;最后记得cleanup中清理下. 

EasyMock.expect(storeMock.getPrice("Mead Spiral Bound Notebook, College Rule")).andReturn(5.99); 

简单来说,mock系列框架的大概原理就这样。更多用法可以参考官网:http://easymock.org/

原文地址:https://www.cnblogs.com/lingzeng86/p/6816765.html