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.
对于实际使用的间谍,对price(listOfProductsToPrice)
的调用price(listOfProductsToPrice)
需要转到spyPricingSession
实例spyPricingSession
List<PricingResult<?>> pricedProducts = spyPricingSession.price(listOfProductsToPrice);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.