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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:51:51+00:00 2026-06-01T14:51:51+00:00

How to inject method (using reflection) into super class without extending tested class? I

  • 0

How to inject method (using reflection) into super class without extending tested class?
I need to replace getExternalData() to test the logic inside someMethod(). I cannot change tested classes – because of that I was thinking about injecting method.

I have flowing classes:

public class B extends A{
  public String someMethod(){
    String someString = super.getExternalData();
    //logic to be tested - manipulating on someString
    return someString;
  }
}

public class A{
  public String getExternalData(){
    //some Logic that generates "externalData" string
    return externalData;
  }
}

And test:

public class CesTest{
  @Test
  public void someMethodTest()(
    B instance = new B();
    //...
    //...
    assertEquals("expectedData", instance.someMethod());
  }
}
  • 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-01T14:51:53+00:00Added an answer on June 1, 2026 at 2:51 pm

    Simple answer is you can’t without doing some really odd stuff that would result in your not testing the class under test but some odd mocked-out version of the class under test. You need to do whatever setup is required to have getExternalData return the appropriate value. That being said, class A should be written in such a was that a mock could be injected to provide this external data without hitting an external resource.

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

Sidebar

Related Questions

I am trying to inject 'GraauditService' into grails 'User' domain class. I am using
I'm trying to inject Spring beans into an EJB using @Interceptors(SpringBeanAutowiringInterceptor.class) but I cannot
I'm using StructureMap to inject a LINQ data context ( DB class) into my
I wish to inject an object file into an existing binary. The method I
Is is possible to inject IPrincipal using Castle Windsor into my asp.net mvc controller.
I'd really appreciate some advice around testing for errors using OCMock. Method Under Test
I want to inject my Java class into an existing Java application, on Windows.
I'm using a DLL-injector to inject a dll which goes into the IAT and
I'd like to inject jQuery into a page using the Google AJAX Libraries API,
How to inject object by using just annotations in parent class by using getter/setter

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.