且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Mockito - 返回与传入方法相同的对象

更新时间:2023-11-30 22:41:16

您可以实现一个 Answer,然后使用 thenAnswer() 代替.

You can implement an Answer and then use thenAnswer() instead.

类似于:

when(mock.someMethod(anyString())).thenAnswer(new Answer() {
    public Object answer(InvocationOnMock invocation) {
        return invocation.getArguments()[0];
    }
});

当然,一旦你有了这个,你就可以将答案重构为一个可重用的答案,称为 ReturnFirstArgument 或类似的.

Of course, once you have this you can refactor the answer into a reusable answer called ReturnFirstArgument or similar.