简体   繁体   中英

Mockito.doReturn().when() is not working - the unit test keeps on calling the original method

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM