应用程序挂起模拟方法调用

Application hangs on mocked method call

我想嘲笑我的 AuthenticationManager class。我这样做如下:

AuthenticationManager authManager = Mockito.mock(AuthenticationManager.class);

我只想模拟其中一种方法,PerformSignInAsync。这个方法returnsvoid。传递给此方法的参数之一是处理程序,它需要调用其 onComplete 事件。我正在尝试使用 ArgumentCaptor 如下所示:

ArgumentCaptor<AuthenticationResponseHandler> authResponseCaptor = ArgumentCaptor.forClass(AuthenticationResponseHandler.class);

下面是我如何模拟我想要模拟的方法。当测试到达真正的方法时,我已经使用调试器逐步完成,并且它正在被 Mockito 调用。所以我认为问题一定出在我触发 onComplete 调用上。 一旦真正的 PerformSignInAsync 被调用,应用程序就挂起,没有出现异常。

doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            AuthenticationResponseHandler handler = (AuthenticationResponseHandler) args[4];
            // The line below is what I want triggered
            handler.onComplete(AuthenticationOperation.SignIn, responseToReturn);
            return null;
        }
    }).when(authManager).PerformSignInAsync(
            anyString(),
            anyString(),
            anyBoolean(),
            Matchers.any(UserLOBSystemType.class),
            authResponseCaptor.capture(),
            anyString(),
            anyString());

我也试过用下面的代码触发 onComplete,但没有用:

    authResponseCaptor.capture().onComplete(AuthenticationOperation.SignIn, responseToReturn);

问题原来是我在问题的代码块中引用的 authManager 实例与实际方法中使用的实例不同。结果模拟没有被触发。