Mockito使用
2016-10-14 18:25
316 查看
import static org.mockito.Mockito.*; /** * Created by Administrator on 2015/12/30. */ public class MockTest { protected static final Logger logger = Logger.getLogger(MockTest.class); //@Test public void easyMockTest() { List list = EasyMock.createMock(List.class); // 录制过程 // 期望方法list.set(0,1)执行2次,返回null,不抛出异常 expect1: EasyMock.expect(list.set(0, 1)).andReturn(null).times(2); // 期望方法list.set(0,1)执行1次,返回null,不抛出异常 expect2: EasyMock.expect(list.set(0, 1)).andReturn(1); // 执行测试代码 EasyMock.replay(list); // 执行list.set(0,1),匹配expect1期望,会返回null Assert.assertNull(list.set(0, 1)); // 执行list.set(0,1),匹配expect1(因为expect1期望执行此方法2次),会返回null Assert.assertNull(list.set(0, 1)); // 执行list.set(0,1),匹配expect2,会返回1 Assert.assertEquals(1, list.set(0, 1)); // 验证期望 EasyMock.verify(list); Integer i1 = 100; Integer i2 = 100; if (i1 == i2) System.out.println("i1 and i2 is equal"); else System.out.println("i1 and i2 is not equal "); byte a = 127; byte b = 127; byte c = 0; c = (byte) (b + a); System.out.println(c); // error : cannot convert from int to byte b += a; // ok System.out.println(b); System.out.println(3 * 0.1); } @Test public void mockitoTest() { //也可以Mock具体的类,而不仅仅是接口 LinkedList mockedList = mock(LinkedList.class); //Stub when(mockedList.get(0)).thenReturn("first"); // 设置返回值 when(mockedList.get(1)).thenThrow(new RuntimeException()); // 抛出异常 //第一个会打印 "first" System.out.println(mockedList.get(0)); //接下来会抛出runtime异常 System.out.println(mockedList.get(1)); //接下来会打印"null",这是因为没有stub get(999) System.out.println(mockedList.get(999)); // 可以选择性地验证行为,比如只关心是否调用过get(0),而不关心是否调用过get(1) verify(mockedList).get(0); } @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void exceptionTest() { thrown.expect(RuntimeException.class); thrown.expectMessage("Runtime exception occurred"); throw new RuntimeException("Runtime exception occurred"); }
相关文章推荐
- PowerMockito使用详解
- Mockito教程-简单使用
- Mockito 使用注解来初始化的代码片断 例子
- junit与testng 分别和mockito 结合使用例子
- Mockito中ArgumentCaptor的使用方法
- 使用Junit和mockito写单测的一些注意点
- 提升单元测试体验的利器--Mockito使用总结
- PowerMockito使用详解
- 测试框架Mockito使用笔记
- 使用 Mockito 的 @InjectMocks 创建被测试类实例
- PowerMockito使用详解
- Mockito入门使用一例
- Mockito使用注意事项
- Mock 模拟测试简介及 Mockito 使用入门
- mockito使用心得
- PowerMockito使用详解
- 使用Spring+Junit+Mockito做代码自测
- Android 单元测试 Mockito使用详解
- Mockito和PowerMockito的使用
- 基于JUnit使用PowerMock的Mockito扩展在Maven测试项目中的配置说明