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 7715521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:24:22+00:00 2026-06-01T02:24:22+00:00

I have a class that implements InvocationHandler as below: public class MyProxyClass implements InvocationHandler

  • 0

I have a class that implements InvocationHandler as below:

public class MyProxyClass implements InvocationHandler
{
  public Object invoke (Object proxy, Method method, Object[] args) throws Throwable
  {
    //Do something interesting here
  }
}

Using PowerMock & Mockito, I’m trying to pass in a mocked method object in my unit test class:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Method.class})
public class MyProxyTest
{
  MyProxy underTest;

  @Test
  public void testInvoke() throws Throwable
  {
    Method mockMethod = mock(Method.class);
    //...
  }
}

Since Method is final, I’ve done the @PrepareForTest trick but that doesn’t seem to cut it. Is this because it’s bootstrapped? Am I just going about this wrong?

I’ve been looking at the below links but there’s nothing definitive there:

  • https://code.google.com/p/powermock/wiki/MockitoUsage13
  • https://code.google.com/p/powermock/wiki/MockSystem
    • This one seems more geared toward static methods than creating a mock of a final class.
  • 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-01T02:24:23+00:00Added an answer on June 1, 2026 at 2:24 am

    With PowerMock you can mock a final class, however, although I don’t believe it’s documented, there are some classes in the java.lang and java.lang.reflect package that you just can’t mock because they are too fundamental to how the mocking framework does it’s thing.

    I think these include (but are probably not limited to) java.lang.Class, java.lang.reflect.Method and java.lang.reflect.Constructor.

    However, what are you trying to do that requires a mock method? You could create a real method object easily enough. You could even create a real method object on a dummy class that you could then check to see if it was ever invoked and with what arguments. You just can’t use Mockito and Powermock to do it. See if your problem is similar to this question.

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

Sidebar

Related Questions

I have a class that implements IEnumerator<string> . See below: public class MyClass :
I have a class that implements singleton pattern as you can see below: public
I have a class that implements IComparable. public class MyClass : IComparable<MyClass> { public
I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have a class that implements IPermission : public class MySecurityPermission : IPermission {
I have a class that implements an interface. The interface is public interface IRiskFactory
I have a class that implements IDisposable public class Foo: IDisposable { public void
Suppose I have a class that implements an interface: public class A implements IB
Straight to the problem: I have a class that implements two interfaces: public class
I have class X that implements Queue. I want to pass Class X's Object

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.