Mockito.doReturn().when() 不起作用 - 单元测试继续调用原始方法

Mat*_*NNZ 5 java mockito

我正在为 SOAP API 编写一个单元测试,其中我需要模拟某个方法的响应,但该方法始终被调用。

我的单元测试的(相关)代码如下:

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);        
    }
Run Code Online (Sandbox Code Playgroud)

.price()在监视对象pricingSession(类型)的方法内部,PricingSessionImpl调用以下方法:

protected List<MxDocument> send(MxML mxml, String action, long timeout, TimeUnit timeoutUnit) throws RequestException, RequestTimeoutException 
Run Code Online (Sandbox Code Playgroud)

该方法的实现在父类中找到public abstract class AbstractPricingSession(但该方法本身不是abstract),您可以在下面找到层次结构:

在此输入图像描述

当我调试这个单元测试时,在某些时候我会调用我想要模拟的方法:

List<MxDocument> documents = send(mxml, ACTION_PRICE, getTimeoutDuration(), getTimeoutUnit());
Run Code Online (Sandbox Code Playgroud)

在这里,我希望 myMockito返回 me ,因为对类中myResult方法的调用是通过类型参数完成的,然后是一,一和一,这正是我传递给.send()PricingSessionImplMxMLStringlongTimeUnit.when()

然而,该方法不断被调用。

谁能指出我调试这个问题的好方向?请注意,我已经检查了网络上已有的有关此主题的多个问题/答案,但到目前为止没有发现任何对我的具体案例有帮助的内容。
如果您需要在代码中查看更多内容,请随时询问。

Mat*_*atF 4

为了实际使用间谍,price(listOfProductsToPrice)需要调用到被监视的实例spyPricingSession

List<PricingResult<?>> pricedProducts = spyPricingSession.price(listOfProductsToPrice);
Run Code Online (Sandbox Code Playgroud)