简体   繁体   English

静态方法的 doAnswer - PowerMock

[英]doAnswer for static methods - PowerMock

One of static method I am using, it does two things.我正在使用的一种静态方法,它做两件事。 It returns some data, but it also modifies the argument object that is passed to it.它返回一些数据,但它也会修改传递给它的参数对象。 This updated argument object is then used later in code.这个更新的参数对象随后会在代码中使用。

I am using PowerMock to mock the return behavior.我正在使用 PowerMock 来模拟返回行为。

For defining the second part - updating the input argument, I am defining doAnswer method but it's not working.为了定义第二部分 - 更新输入参数,我正在定义 doAnswer 方法,但它不起作用。 The method that I'm trying to test looks like this.我尝试测试的方法如下所示。

public void login() throws ConnectionException, AsyncApiException {
    ConnectorConfig partnerConfig = new ConnectorConfig();

    //This call sets the value in one member variable 'serviceEndPoint in ParterConfig which is accessed later in this method only.
    partnerConnection = Connector.newConnection(partnerConfig);

    //partnerConfig.getServiceEndpoint is called.

    PowerMockito.mockStatic(Connector.class);
    when(Connector.newConnection(Mockito.any(ConnectorConfig.class))).thenReturn(partnerConnection);

    PowerMockito.doAnswer(new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocation) {
            ConnectorConfig config = (ConnectorConfig) invocation.getArguments()[0];
            config.setServiceEndpoint("service end point");
            return null;
        }
    }).when(Connector.newConnection(Mockito.any(ConnectorConfig.class)));
}     

but above throws error saying 'Unfinished stubbing detected here'.但上面会抛出错误,提示“此处检测到未完成的存根”。 Connector is a third party class so I don't have control over its behavior. Connector是第三方类,所以我无法控制它的行为。

Any suggestions, what could be going wrong?任何建议,可能会出什么问题?

PowerMockito.doAnswer(new Answer<Void>() {
    /* ... */
}).when(Connector.newConnection(Mockito.any(ConnectorConfig.class)));

Your when is the problem.when有问题。 In normal Mockito, using any doAnswer / doReturn /etc call, you have to place the call you're stubbing outside of the call to when , like so:在普通的 Mockito 中,使用任何doAnswer / doReturn /etc 调用,您必须将要存根的调用置于when调用之外,如下所示:

Mockito.doAnswer(new Answer<Void>() {
    /* ... */
}).when(yourMock).callVoidMethod();
//            ^^^^^^

PowerMockito further requires that calls to static methods happen in the next statement , like so: PowerMockito 进一步要求在下一个语句中调用静态方法,如下所示:

PowerMockito.doAnswer(new Answer<Void>() {
    /* ... */
}).when(Connector.class); Connector.newConnection(/*...*/);
//                    ^^^^^^

Note that from 2009 (~1.3.x) to 2013 (~1.5.x) , the docs were actually inconsistent, alluding to a zero-arg when despite all signatures requiring at least a class literal.请注意,从2009 (~1.3.x)2013 (~1.5.x) ,文档实际上是不一致的,尽管所有签名都需要至少一个类文字when但暗指零参数。

Obligatory PSA: It's generally a good idea to avoid mocking types you don't own , though opinions may vary on that one .强制性 PSA: 避免嘲笑您不拥有的类型通常是一个好主意,尽管对该类型的看法可能会有所不同

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

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