lundi 27 juin 2016

Application hangs on mocked method call

I'm trying to mock my AuthenticationManager class. I do so as below:

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

I only want to mock one of its methods, PerformSignInAsync. This method returns void. One of the arguments passed to this method is a handler and it needs to have its onComplete event called. I am trying to do so with an ArgumentCaptor as below:

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

Below is how I've approached mocking the method I want mocked. When the test reaches the real method I've stepped through with the debugger and it is being invoked by Mockito. So I think the issue must be with my triggering of the onComplete call. The application just hangs with no exception raised once the real PerformSignInAsync has been called.

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());

I've also tried triggering onComplete with the below code, to no avail:

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

Aucun commentaire:

Enregistrer un commentaire