Using Mockito, how do I intercept a callback object on a void method?

后端 未结 1 1931
长发绾君心
长发绾君心 2020-12-28 13:21

I\'m using mockito to test a legacy JAAS/LDAP login module.

The javax.security.auth.callback.CallbackHandler interface defines the function:

<         


        
相关标签:
1条回答
  • 2020-12-28 14:18

    For functions returning void, use doAnswer()

    doAnswer(...).when(mockedObject).handle(any(Callback[].class));
    

    And an Answer that performs the interception must go in as the parameter to doAnswer, e.g. as an anonymous class:

    new Answer() {
      public Object answer(InvocationOnMock invocation) {
          Object[] args = invocation.getArguments();
          Mock mock = invocation.getMock();
          return null;
      }}
    

    In this case args will be the array Callback[]!

    0 讨论(0)
提交回复
热议问题