I am writing an unit test for a SOAP API in which I need to mock the response of a certain method, which is however called all the time.
The (relevant) code of my unit test is the following:
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
public class PricingSessionTest{
@Test
public void testPricingOfStrategyWithCorrectFormat() throws Exception {
//[...other code...]
PricingSessionImpl pricingSession = new PricingSessionImpl(this.session);
PricingSessionImpl spyPricingSession = Mockito.spy(pricingSession);
Mockito.doReturn(myResult)
.when(spyPricingSession)
.send(
Mockito.any(MxML.class),
Matchers.eq(ACTION_PRICE),
Matchers.eq(TimeoutDuration),
Matchers.eq(TimeoutUnit)
);
List<PricingResult<?>> pricedProducts = pricingSession.price(listOfProductsToPrice);
}
Inside the method .price()
of the spied object pricingSession
(type PricingSessionImpl
) there is a call to the following method:
protected List<MxDocument> send(MxML mxml, String action, long timeout, TimeUnit timeoutUnit) throws RequestException, RequestTimeoutException
The implementation of the method is found in the parent class public abstract class AbstractPricingSession
(but the method itself is not abstract
), you can find the hierarchy below:
When I debug this unit test, at some point I get in front of a call to the method I want to mock:
List<MxDocument> documents = send(mxml, ACTION_PRICE, getTimeoutDuration(), getTimeoutUnit());
In here, I would expect my Mockito
to return me myResult
, since the call to the method send()
in the class PricingSessionImpl
is done with a parameter of type MxML
and then one String
, one long
and one TimeUnit
which are exactly what I'm passing to the .when()
.
However, the method keeps on getting called.
Can anyone point me towards the good direction to debug this issue? Please note I've checked the multiple questions/answers on this subject already present in the web, but didn't find anything helpful for my specific case so far.
In case you need to see more in the code, don't hesitate to ask.
For the spy to actually be used, the call to price(listOfProductsToPrice)
needs to go to the spied on instance spyPricingSession
List<PricingResult<?>> pricedProducts = spyPricingSession.price(listOfProductsToPrice);