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

时间:2016-06-27 18:26:37

标签: java android unit-testing mockito void

我正在试图模仿我的AuthenticationManager课程。我这样做如下:

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

我只想模仿其中的一个方法PerformSignInAsync。此方法返回void。传递给此方法的参数之一是处理程序,它需要调用其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);

1 个答案:

答案 0 :(得分:0)

问题原来是我在我的问题中的代码块中引用了authManager的不同实例,而不是实际方法中使用的实例。因此,模拟没有被触发。

相关问题