简体   繁体   English

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

[英]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. 我正在为SOAP API编写一个单元测试,我需要在其中模拟某个方法的响应,然而这个方法一直被调用。

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: 在spied对象pricingSession (类型PricingSessionImpl )的方法.price()中,有一个对以下方法的调用:

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: 该方法的实现可以在父类public abstract class AbstractPricingSession (但方法本身不是abstract ),您可以在下面找到层次结构:

在此输入图像描述

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() . 在这里,我希望我的Mockito能够返回myResult ,因为在类PricingSessionImpl对方法send()PricingSessionImpl是使用MxML类型的参数完成的,然后是一个String ,一个long和一个TimeUnit ,这正是我的' m传递给.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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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