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

  • SEARCH
  • Home
  • 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 7793673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:33:43+00:00 2026-06-01T22:33:43+00:00

my first question on StackOverflow. I’d like to be able to do something like:

  • 0

my first question on StackOverflow. I’d like to be able to do something like:

SomeClass mock = mock(SomeClass.class);

String methodName = “someMethod”; OR Method method = …someMethod…

Both of these things (the mock and the method) would combine to do the following:

when(mock.someMethod()).thenReturn(null);

Of course, the ‘null’ value will be changed accordingly for my needs, but I am trying to determine two things:

1) Is it even possible to do something like this in Java? This = combining a class object and a method into a methodCall.

2) How do I do such a thing?

I’ve researched this endlessly and I can’t find anything. The problem is that even if this works with a regular class and a regular method (someClass and someMethod would come together to do someClass.someMethod()), keep in mind that this has to work with a mock object for use inside a when() call.

ANSWERED: when(method.invoke(mock)).thenReturn(“Hello world.”); is the correct syntax and reflection indeed does work inside a when() call. Thanks Kevin Welker!

  • 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-06-01T22:33:45+00:00Added an answer on June 1, 2026 at 10:33 pm

    Since you basically asked me to repost my comment, modified by your response, as an answer, here it is:

    Try using reflection as in:

    when(method.invoke(mock)).thenReturn("Hello world.");
    

    although, I’m not sure how this is working for you, since you cannot mock/spy class Method (it is final). Mockito’s when() only works on mocks or spies. If this is really working for you, can you post a little more detail?

    If it doesn’t work, you can — as I suggested in my comment in the OP — go the CGLib route and bypass Mockito. It’s really not that difficult as it looks at first. In my OSS project Funcito (not a mocking framework), I stripped down a lot of the Mockito CGLib proxying code and rewrote it for my needs. It gives a much simpler view into the world of proxying classes, and intercepting method calls.

    ADDITIONAL RESPONSE TO COMMENTS
    I see how this is working for you, but I am not sure you really understand how it is working. The reason this might matter is because future changes to the way Mockito itself works could render your solution broken in the future. In essence, the reason it works is almost accidental, but yes it will work.

    The way that when() is supposed to work is that what happens in between the parentheses is a method call on a previously created Mockito-generated mock or spy, which is just a fancy proxy of a class, rather than a real instance of the class. The proxies have special logic that intercepts the fake proxied method call and basically add that to a list of registered proxy-method invocations (it is stored in something called an IOngoingStubbing or something like that) for later use. Since Java evaluates parameters before invoking a method, this guarantees that the proxied method call gets registered/remembered before the when() method is actually executed. What the when() does is pops off this IOngoingStubbing, which then becomes the object on which thenReturns() is called.

    You are not using this “correctly” but it still works for you. How? Well, all that needs to happen is that a method on the proxy needs to be called in order to be registered in a IOngoingStubbing before when() gets executed. You are not directly invoking a method on a proxy, but you are indirectly invoking a method on a proxy by passing the proxy to Method.invoke(). Hence the criteria is satisfied, and when() already has a proxy-method-call registered in an IOngoingStubbing.

    You can see the same kind of “accidental” happiness in the following code, which appears at first to make no sense until you realize how Mockito works:

    @Test
    public void testSomething() throws Exception {
        List listMock = mock(List.class);
        Method m = List.class.getDeclaredMethod("get", int.class);
        m.invoke(listMock, Mockito.anyInt());
    
        when(null).thenReturn("Hello World");  // Huh? passing null?
    
        assertEquals("Hello World", listMock.get(0)); // works!
    }
    

    The above test actually passes! Even though the argument to when is null, what counts is that the proxy (i.e., mock) instance had the correct method invoked on it prior to the when statement being invoked.

    While it is unlikely that Mockito will change the basic way things work under the covers, there is still the potential for this to break for you sometime in the future. Like I said, it is more or less a happy accident that it works. As long as you understand the way it works and the risk involved, more power to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first question on stackoverflow (I have usually been able to figure
this is my first question on StackOverflow, but I think that we'll both come
First question on Stackoverflow (.Net 2.0): So I am trying to return an XML
this is my first question to stackoverflow so here it goes... I use cruise
This is my first question on stackoverflow. I just wonder why my getJSON code
Greetings to all! This is my first question here on stackoverflow. I have a
First of all, sorry if this isn't an appropriate question for StackOverflow. I've tried
First up, my question is very similar to questions asked in Stackoverflow (and the
This is my first here in Stackoverflow. So I just want to ask question
First question on stackoverflow :) Hope I won't embarrass myself... I have a javascript

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.