doAnswer for static methods - PowerMock

2019-03-20 02:29发布

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.

For defining the second part - updating the input argument, I am defining doAnswer method but it's not working. 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.

Any suggestions, what could be going wrong?

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-03-20 02:54
PowerMockito.doAnswer(new Answer<Void>() {
    /* ... */
}).when(Connector.newConnection(Mockito.any(ConnectorConfig.class)));

Your when is the problem. 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(new Answer<Void>() {
    /* ... */
}).when(yourMock).callVoidMethod();
//            ^^^^^^

PowerMockito further requires that calls to static methods happen in the next statement, like so:

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

Note that the documentation I linked is actually inconsistent--looks like the docs allude to a zero-arg when, whereas the class literal is required in the signatures available. That might be a good thing to flag as a bug, if you have a moment.

Obligatory PSA: It's generally a good idea to avoid mocking types you don't own, though the jury's still out on that one.

查看更多
登录 后发表回答