Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The following should work:

when(myObjectRepository.save(any(MyObject.class)))
     .then(AdditionalAnswers.returnsFirstArg());
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!