Current Status

Actively playing with all possible backend services

12 February, 2018

Mockito to execute or not a method


This is well known thing if you are using Mockito to mock your services. But I wanted to note it here.

 Imagine you have a method call inside method you are writing the unit test (or eg:-getPayment() ) that should not be called when your test is executed.
 That may be the first reason why you mocked it in the first place.

 In below code, I want to mock getPayment() method without executing it.

option A:
    Mockito.when(subject.getPayment()).thenReturn(paymentDetail);

option B:
    Mockito.doReturn(paymentDetail).when(subject).getPayment();

If you tested both, option A will actually call the getPayment()- method while the second will not. Both will cause doSomeStuff() to return the desired `paymentDetail` Object.

Hope this helps someone new ith Mockito!


No comments:

Some Popular Posts