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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:18:26+00:00 2026-06-01T16:18:26+00:00

Is it possible using Mockito and optionally Powermock to mock a superclass S such

  • 0

Is it possible using Mockito and optionally Powermock to mock a superclass S such that any calls to the superclass to S (including calls to the S() constructor) are mocked? So using the below example, if I replace S with MockS using Mockito, will the call to super() use the constructor in MockS?

class S {
   S() {
      // Format user's hard drive, call 911, and initiate self-destruct
   }
}

class T extends S {
   T() {
      super();
   }
}

class Test {
   @Mock private S mockS;
   new T(); // T's call to super() should call the mock, not the destructive S.
}

I’ve seen questions about mocking individual methods in S or mocking only calls to super(), and read that this is unsupported, but it’s not clear whether or not I can mock the entire superclass.

With my current tests, when I try to mock S, T‘s call to super() calls the real implementation, not the mock.

  • 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-01T16:18:27+00:00Added an answer on June 1, 2026 at 4:18 pm

    To work around this apparent limitation, I refactored my code, replacing inheritance with delegation, and I think I’ve ended up with a better design anyhow since the inheritance wasn’t really necessary.

    The new code looks like this. Mind you the code for the question was simplified, so the real classes have much more functionality.

    class S {
       S() {
          // Format user's hard drive, call 911, and initiate self-destruct
       }
    }
    
    class T {
       T(S s) {} // Now T "has an S" instead of "is an S"
    }
    
    class Test {
       @Mock private S mockS;
       new T(s); // T's call to super() should call the mock, not the destructive S.
    }
    

    For those interested, using Guice and Android, the test looks more like this:

    class T {
       T(Activity activity, S s) {}
    }
    
    class Test {
      @Mock Activity activity;
      @Mock S mockS;
      injector = Guice.createInjector(new AbstractModule() {
         @Override protected void configure() {
            bind(Activity.class).toInstance(activity);
            bind(S.class).toInstance(mockS);
         }}
      );
      T t = injector.getInstance(T.class);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JUnit with Mockito. PowerMock can mock static methods but it doesn't seem
Is it possible (using HTML only) to display select with option s that span
I have a feeling that this probably is not possible using strictly CSS, but,
Is it possible using jQuery, or any other language, to make an input's value
I have a problem with Mockito. Is it possible to do such a thing:
I'm just wondering is that even possible using forms and PHP? Thanks.
Is it possible using jQuery (or any other JavaScript function) to detect when an
Is it possible to fetch/store what arguments were used when using Mockito.when? For example
Is below possible using WSO2 IS? I have gone through different WSO2 blogs. They
I am using mockito and trying to mock a scala object. object Sample {

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.