Mock一个函数的逻辑

By | 2015年12月31日

不管是mock的实例,还是spy的实例,都可以用Mockito.doAnswer()。

List list = mock(List.class);
//List list = spy(new ArrayList());
Object obj = mock(Object.class);
doAnswer(new Answer<Boolean>() {
    @Override
    public Boolean answer(InvocationOnMock invocation) throws Throwable {
    return true;
  }
}).when(list).contains(obj);
assertTrue(list.contains(obj));