easyMock简单入门例子 .

来源:互联网 发布:php aes 解密 编辑:程序博客网 时间:2024/05/16 04:58

接口:

 

[java] view plaincopyprint?
  1. package com.inter;  
  2. public interface AddInter {  
  3. public int add(int a,int b);  
  4. }  
 

测试类:
[javascript] view plaincopyprint?
  1. package com.service;  
  2. import static org.junit.Assert.*;  
  3. import org.easymock.EasyMock;  
  4. import org.junit.AfterClass;  
  5. import org.junit.Before;  
  6. import org.junit.BeforeClass;  
  7. import org.junit.Test;  
  8. import com.inter.AddInter;  
  9. public class AddServiceTest {  
  10.     private AddInter addInter;  
  11.     @Before  
  12.     public  void setUp() throws Exception {  
  13.         addInter = EasyMock.createMock(AddInter.class);  
  14.           
  15.     }  
  16.     @Test  
  17.     public void testAdd() {  
  18.         EasyMock.expect(addInter.add(1, 1)).andReturn(2);  
  19.         EasyMock.expect(addInter.add(1, 2)).andReturn(3);  
  20.           
  21.         EasyMock.replay(addInter);  
  22.           
  23.           
  24.           
  25.         assertEquals(2,addInter.add(1, 1));  
  26.         assertEquals(3,addInter.add(1, 2));  
  27.           
  28.           
  29.         EasyMock.verify(addInter);  
  30.           
  31.     }  
  32. }  
原创粉丝点击