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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:56:02+00:00 2026-05-26T12:56:02+00:00

What I wanna do is a method that can generate instance of Class X

  • 0

What I wanna do is a method that can

  • generate instance of Class X (a class variable passed in arg) and
  • override some of it’s method

More specifically, the parent class X I want to override contains

  • Contains no default constructor (e.g. all constructors with args)
  • Constructors calling non-private method within the same class

Originally I thought it’s quite simple to use reflection or something similar,
Then I found there’s limitation on implementing my requirement.

  • For refection: Can only override “interface” via java.lang.reflect.Proxy
    http://download.oracle.com/javase/1.3/docs/guide/reflection/proxy.html
  • for cglib: it cannot create instance of no default constructor and constructor calling non-private member methods
    http://insufficientinformation.blogspot.com/2007/12/spring-dynamic-proxies-vs-cglib-proxies.html

I think this is achievable, since Mockito can do all kinds of method injection runtime.
Please anyone give some advise, Thanks.
The pseudo-code I image is like this:

createAndOverride(Class X) {
    X newObj = X.newInstance(args) {
        @override
        methodOfX(args2) {
            ...
        }
    }
    return newObj;
}
  • Original problem scenario

I was intended to test a Class which has several methods calling X1.get(), X2.get(), X3.get()
In some test case, I need to make Xn.get() to return something I can control for test (e.g. null)
Due to below constraint:

  • But due to mock tool restriction to JMock 1.0 (I have no control 🙁 ), so I cannot just simply mock Xn.get() to returns “someSpecifiedObjects”
  • Xn has no null constructors and constructors calling non-private member

My workaround is self made Xn Class and pass them to test case to let Cn.get() to be expected
code example:

ClassToTest.SomeMethod(new X1() {
    @override
    get() {
        return someSpecifiedObjects;
    }
});

And this kind of thing is spread-ed over the Test Case.
Therefore, In order to reduce duplicate code, I would like to build a method to generate Xn instance with specified overrided method for test. e.g.

X1 x1 = createAndOverride(X1);

Then, the problem of this post comes

  • 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-26T12:56:02+00:00Added an answer on May 26, 2026 at 12:56 pm

    So what I think you need is a similar functionality to C#’s Reflection.Emit:

    • Using Reflection.Emit to create a class implementing an interface
    • Java Equivalent of Reflection.Emit
    • Dynamically Create Java Classes With JavaClassCreator

    While I haven’t done this myself, I think you should be able to use reflection/emission and dynamic type creation in order to achieve what you’re looking for. However, I would still like to mention that if you’re trying to test “functionality” that’s not int he code path of the function you’re testing, then you probably shouldn’t be testing it at all. For example:

    SomeObjectInterface get()
    {
        if(_someObjectStateIsSet)
        {
            // Return a concrete implementation A
            return new ConcreteImplA();
        }
        else
        {
            // Return a concrete implementation B
            return new ConcreteImplB();
        }
    }
    

    In this case get has no code path that would return null, so you shouldn’t need to test for null. I’m not sure if I understood your question 100% correctly, especially why you’re testing for null, but consider the above advice and see what works for you.

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

Sidebar

Related Questions

I wanna do that: Method will be deleteLeafValue where it will get Object value.
I wanna a method that would loop any type array and print them, I
so I have a class request that has a method call that makes an
I have a method that invokes other method in the same class such as:
I wanna implement a javascript like method in java , is this possible ?
I wanna place two child un-ordered lists side by side. They are having Class
i wanna add a service behavior(or anything u'll suggest) that will automatically insert the
I'm somewhat familiar with WCF in that I can build Web Services in VS.Net
What s the best way to implement a method that creates and assings ID
I got a requirement in my project to add another property to some class.

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.