How to use ArgumentCaptor for stubbing?

前端 未结 3 910
礼貌的吻别
礼貌的吻别 2020-11-27 11:51

In Mockito documentation and javadocs it says

It is recommended to use ArgumentCaptor with verification but not with stubbing.

3条回答
  •  时光取名叫无心
    2020-11-27 12:29

    Hypothetically, if search landed you on this question then you probably want this:

    doReturn(someReturn).when(someObject).doSomething(argThat(argument -> argument.getName().equals("Bob")));
    

    Why? Because like me you value time and you are not going to implement .equals just for the sake of the single test scenario.

    And 99 % of tests fall apart with null returned from Mock and in a reasonable design you would avoid return null at all costs, use Optional or move to Kotlin. This implies that verify does not need to be used that often and ArgumentCaptors are just too tedious to write.

提交回复
热议问题