How to return the argument, which the mock method was called with, as response?

Viewed 123

Is it possible in Mockito to return the object that mocked method was called with? Without prior knowing what object it will be.

@Mock
MyObjectRepository myObjectRepository;
...
when(myObjectRepository.save(any(MyObject.class))) //save method returns normally MyObject.class object
     .thenReturn(\\the object that method was called with);

I want to return the object that is passed to save method.

1 Answers

The following should work:

when(myObjectRepository.save(any(MyObject.class)))
     .then(AdditionalAnswers.returnsFirstArg());
Related