Mockito spy

You are currently viewing Mockito spy

A mockito spy could be a partial mock. we will mock part of the item by stubbing a few methods, while real method invocations are used for the opposite. By saying so, we will conclude that calling away on a mockito spy will invoke the particular method, unless we explicitly stub the tactic, and thus the term partial mock.

mockito spy

Mockito spy Example

Let’s start with an easy example of a way to use a mockito spy.

Simply put, the API is Mockito.spy() to spy on a true object.

This will allow us to call all the traditional methods of the item while still tracking every interaction, even as we’d with a mock. OK, let’s do a fast example where we’ll spy on an existing ArrayList object:

@Test
public void whenSpyingOnList_thenCorrect() {
    List<String> list = new ArrayList<String>();
    List<String> spyList = Mockito.spy(list);
 
    spyList.add("one");
    spyList.add("two");
 
    Mockito.verify(spyList).add("one");
    Mockito.verify(spyList).add("two");
 
    assertEquals(2, spyList.size());
}

mockito spy Annotation

Next – let’s have a look at a way to use the Spy annotation. we will use Spy annotation rather than spy() as within the following example:

@Spy
List<String> spyList = new ArrayList<String>();
 
@Test
public void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
    spyList.add("one");
    spyList.add("two");
 
    Mockito.verify(spyList).add("one");
    Mockito.verify(spyList).add("two");
 
    assertEquals(2, spyList.size());
}

In order to enable Mockito annotation (such as @Spy, @Mock, … ) – we’d like to try to to one in every of the following:

  1. Call the strategy MockitoAnnotations.initMocks(this) to initialize annotated fields
  2. Use the built-in runner @RunWith(MockitoJUnitRunner.class)

Stubbing a mockito spy

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());
}

Mock vs. Spy in Mockito

In the following example – we create a mock of the ArrayList class:



@Test
public void whenCreateMock_thenCreated() {
    List mockedList = Mockito.mock(ArrayList.class);
 
    mockedList.add("one");
    Mockito.verify(mockedList).add("one");
 
    assertEquals(0, mockedList.size());
}

As we will see – adding a component into the mocked list doesn’t actually add anything – it just calls the strategy with no other side-effect.

A spy on the opposite hand will behave differently – it’ll actually call the important implementation of the add method and add the element to the underlying list:\

@Test
public void whenCreateSpy_thenCreate() {
    List spyList = Mockito.spy(new ArrayList());
 
    spyList.add("one");
    Mockito.verify(spyList).add("one");
 
    assertEquals(1, spyList.size());
}

Visit the java website for more information. Visit

Visit the java tutorial list. And make strong your java concept. Click here. wuschools.com is always written about the CSS concept for the java lover. And writes about how java makes your life easy if you are a web site developer. We help you to continue your learning.

Leave a Reply