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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:33:00+00:00 2026-05-25T18:33:00+00:00

I want to create a method in superclass of test-class that stubs some commonly

  • 0

I want to create a method in superclass of test-class that stubs some commonly used methods in under-test-classes but some of those methods might not exist.

For example, I have a class hierarchy like this:

abstract class A {
    void search(); // implemented by subclass

    String getFoo() { return "REAL FOO"; }
}

class B extends A {
    void search() {
        getFoo();
    }   
}

class C extends A {
    void search() {
        getFoo();
        getBar();
    }   

    String getBar() { return "REAL BAR"; }
}

There are tons of subclasses of A (a tool generated the skeleton) thus I want to create a superclass to make it easier for me to test:

abstract class AbstractSearchTest {
    A underTest;

    @Test void test() {
        doReturn( "FOO" ).when( underTest ).getFoo();
        doReturn( "BAR" ).when( underTest, "getBar" ); // THE PROBLEM!

        underTest.search();
    }
}

class BSearchTest extends AbstractSearchTest {
    BSearchTest() {
        underTest = new B();
    }
}

class CSearchTest extends AbstractSearchTest {
    CSearchTest() {
        underTest = new C();
    }
}

Which basically says, “Before invoking search(), stub getFoo(). Oh, if the subclass happen to have getBar(), stub it too.”
But I can’t do that since it’ll throw org.powermock.reflect.exceptions.MethodNotFoundException. How to do this?

  • 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-25T18:33:01+00:00Added an answer on May 25, 2026 at 6:33 pm

    Use reflection to determine if the class is implemented.

    try{
        Method m = underTest.getClass().getMethod("getBar");
        // no exception means the method is implememented
        // Do your mocking here
        doReturn( "BAR" ).when( underTest, "getBar" );
    }catch(NoSuchMethodException e){}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a substr method in C++ in a string class that
I want to create a method that compares a number but can have an
I want to create a class that has one method that calls all other
I want to create a method sum that I can call on different types,
I want to create a helper method that I can imagine has a signature
I want to create a generic method to serizlize a class to text (for
Hi I want to create a WCF service that have login method, which is
I want to create a list of methods to execute. Each method has the
I have a method that's about ten lines of code. I want to create
I want to create one method and call the created Thread inside that method

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.