mockito return value based on property of a parameter

前端 未结 5 1625
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 19:57

Normally when using mockito I will do something like

Mockito.when(myObject.myFunction(myParameter)).thenReturn(myResult);

Is it possible to do

5条回答
  •  被撕碎了的回忆
    2021-01-30 20:15

    You can do this with Mockito 3.6.0:

    when(mockObject.myMethod(anyString()))
        .thenAnswer(invocation -> myStringMethod(invocation.getArgument(0)));
    

    This answer is based on Sven's answer and Martijn Hiemstra's comment, with getArgumentAt() changed to getArgument().

提交回复
热议问题