mockito: Is there a way of capturing the return value of stubbed method?
问题 If I mock a method to return a new instance of some object, how can I capture the returned instance? E.g.: when(mock.someMethod(anyString())).thenAnswer(new Answer() { Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); return new Foo(args[0]) } }); Obviously, I can have a field of type Foo and inside answer set it to the new instance, but is there a nicer way? Something like ArgumentCaptor? 回答1: Looks like you want to