概述
In this post, we are going to see more functionalities in Mockito.
Mock a List interface:
@Test public void testZie_returnOnce() { List listMock = mock(List.class); when(listMock.size()).thenReturn(10); assertEquals(10, listMock.size()); }
Call multi times:
@Test public void testZie_returnMultis() { List listMock = mock(List.class); when(listMock.size()).thenReturn(10).thenReturn(20); assertEquals(10, listMock.size()); assertEquals(20, listMock.size()); }
First call, return 10, second times return 20.
Mock, List.get():
@Test public void testZie_returnGet() { List listMock = mock(List.class); when(listMock.get(0)).thenReturn("Good"); assertEquals("Good", listMock.get(0)); assertEquals(null, listMock.get(1)); }
Mockito.anyInt():
@Test public void testZie_returnGetAny() { List listMock = mock(List.class); when(listMock.get(Mockito.anyInt())).thenReturn("Good"); assertEquals("Good", listMock.get(0)); assertEquals("Good", listMock.get(1)); }
转载于:https://www.cnblogs.com/Answer1215/p/10758734.html
最后
以上就是完美含羞草为你收集整理的[Mockito] Mock List interface的全部内容,希望文章能够帮你解决[Mockito] Mock List interface所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复