Example of Mockito's argumentCaptor

后端 未结 4 1152
我在风中等你
我在风中等你 2020-12-04 07:21

Can anyone please provide me an example showing how to use the org.mockito.ArgumentCaptor class and how it is different from simple matchers that are p

4条回答
  •  渐次进展
    2020-12-04 07:49

    The steps in order to make a full check are:

    Prepare the captor :

    ArgumentCaptor someArgumentCaptor = ArgumentCaptor.forClass(SomeArgumentClass.class);
    

    verify the call to dependent on component (collaborator of subject under test) times(1), is the default value, so ne need to add it.

    verify(dependentOnComponent, times(1)).send(someArgumentCaptor.capture());
    

    times(1) is the default, so no need to add it.

    Get the argument passed to collaborator

    SomeArgumentClass someArgument = messageCaptor.getValue();
    

    someArgument can be used for assertions

提交回复
热议问题