@Mockito.spy(ClassName.class)
It is used to create a spy object in a unit test for any Class or Interface.
Note: spy Object created for interface using spy will be useless because you could not test the actual data on it.
It creates a real object and calls real method.
When we do not stub the method it calls the real method on the creates spy object.
While @Mock creates a completely mock or fake object. It doesnt call the actual real method.
@Test
public void test_ArrayList_With_spy_Method(){
ArrayList<String> list = Mockito.spy(ArrayList.class);
list.add("good");
list.add("morning");
Mockito.verify(list).add("good");
Mockito.verify(list).add("morning");
Assert.assertEquals(2, list.size());
Mockito.doReturn(4).when(list).size();
Assert.assertEquals(4, list.size());
}