或存根方法调用
// you can mock concrete class, not only interfaces
LinkedList mockedList = mock(LinkedList.class);
// stubbing - before execution
when(mockedList.get(0)).thenReturn(“first”);
// following prints “first”
System.out.println(mockedList.get(0));
// following prints “null” because get(999) was not stubbed.
System.out.println(mockedList.get(999));
一个使用Mockito的简单Java代码示例
没有模拟框架
使用Mockito框架
步骤1:在Eclipse中创建一个Maven项目
定义pom.xml
如下:
4.0.0 vn.com.phatbeo.ut.mockito.demo demoMockito 0.0.1-SNAPSHOT jar demoMockito http://maven.apache.org