Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6388293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:15:38+00:00 2026-05-25T03:15:38+00:00

If I mock a method to return a new instance of some object, how

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:15:38+00:00Added an answer on May 25, 2026 at 3:15 am

    Looks like you want to observe and then Answer instances, and receive notifications each time the answer method is called (which triggers the creation of a new Foo). So why not invent an ObservableAnswer class:

    public abstract class ObservableAnswer implements Answer {
      private Listener[] listeners; // to keep it very simple...
    
      public ObservableAnswer(Listener...listeners) {
        this.listeners = listeners;
      }
    
      @Override
      public Object answer(InvocationOnMock invocation) {
        Object answer = observedAnswer(invocation);
        for (Listener listener:listeners) {
           listener.send(answer);
        }
        return answer;
      }
    
      // we'll have to implement this method now
      public abstract Object observedAnswer(InvocationOnMock invocation);
    }
    

    Intended use:

    Listener[] myListeners = getListeners();  // some magic (as usual)
    when(mock.someMethod(anyString())).thenAnswer(new ObservableAnswer(myListeners) {
         Object observedAnswer(InvocationOnMock invocation) {
             Object[] args = invocation.getArguments();
             Object mock = invocation.getMock();
             return new Foo(args[0])
         }
    
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I mock the initialize method on a ruby class? I'm doing some
In NMocks2 you can mock up the result of a method you don't know
I would like to stub the #class method of a mock object: describe Letter
What I wanna do is a method that can generate instance of Class X
I'm trying to use patch to return the a Mock from within a method.
How do I mock the method GetValues() in System.Data.IDataReader? This method changes the array
I've been trying to get to mock a method with vararg parameters using Mockito:
I have a unit test that creates a mock calls my method to be
I am using a mock object in RhinoMocks to represent a class that makes
Trying to mock the following method: bool IsLoginValid(LoginViewModel viewModel, out User user); Tried this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.