mockito return value based on property of a parameter

前端 未结 5 1623
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 20:21

    In Java 8 it is even simpler than all of the above:

    when(mockObject.myMethod(anyString()))
        .thenAnswer(invocation -> 
            invocation.getArgumentAt(0, String.class));
    

提交回复
热议问题