jeudi 3 septembre 2020

Mockito spy is calling the actual class instead of setting the mock

This question is similar to this one but the solution is not working:

public class SomeClass {
   public int A () {
       //...
       this.B(someList);
   }

   public int B(List<Integer> someList) {
        //...
   }
}

I want to unit test A and check the parameters that calls B with. Hence I do:

SomeClass obj = // ... created somehow
SomeClass objSpy = Mockito.spy(obj);

ArgumentCaptor<List<Integer>> captor =
                ArgumentCaptor
                        .forClass(List.class);

doReturn(0).when(objSpy).B(captor.capture());

Instead of mocking B, it is calling the actual implementation of B with null. In the aforementioned question it was suggested that using doReturn would fix this issue, because using when(objSpy.B(captor.capture)).thenReturn(0) would execute the underlying method

Aucun commentaire:

Enregistrer un commentaire