vyacheslav-k
11/28/2017 - 11:46 AM

Mockito. Stubbing a spy.

Now – Let’s see how to stub a Spy. We can configure/override the behavior of a method using the same syntax we would use with a mock.

In the following example – we use doReturn() to override the size() method:

@Test
public void whenStubASpy_thenStubbed() {
    List<String> list = new ArrayList<String>();
    List<String> spyList = Mockito.spy(list);
 
    assertEquals(0, spyList.size());
 
    Mockito.doReturn(100).when(spyList).size();
    assertEquals(100, spyList.size());
}