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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:32:21+00:00 2026-05-25T23:32:21+00:00

Im refactoring some unit tests. Basically, i ve found that unit tests of different

  • 0

Im refactoring some unit tests. Basically, i ve found that unit tests of different clients implement a bundle of methods such as: createClientWithNullResponse, createClientWithSuccessResponse, etc.

I was wondering if its possible in Java to implement a generic solution to this, since this methods are repeated over and over in hundreds of unit classes, changing only the method signature.

But, there is a tricky. See a method example:

/**
 * configures the client to return a succesful response
 * @return a client configured to return a succesful response
 */
private Client1 configureClientWithSuccesfulResponse()
{
    client = new Client1()
    {
        public CommonClientResponse processRequest( CommonsClientRequest commonsClientRequest )
        {
            CommonClientResponse commonClientResponse = new CommonClientResponse();
            commonClientResponse.setResponse( new Client1Response() );
            return commonClientResponse;
        }
    };
    return client;
}

So, client2 will have the very same method except that signature have Client2, and the overrided method creates a new Client2Response, and the same with dozens of clients.

Additional information: processRequest is overrided to act as a mock, setting the response i wish for each method.
Client1 extends CommonsWS that extends of AbstractCommons, which is an abstract class but contains the implementation of processRequest method.

All in all, my idea is to create a Base class for all unit tests, with a bundle of generic methods where i can pass the class type, and then rewrite the processRequest for each one. Ive tried :

public class base <T extends AbstractCommonClient>{

    private T configureClientWithNullResponse(Class <? extends AbstractCommonClient> clazz, Class< ? extends ClientResponse> clazz1)
    {

        try
        {
            return clazz.newInstance()
            {
                CommonClientResponse processRequest( CommonsClientRequest commonsClientRequest )
                {
                    CommonClientResponse commonClientResponse = new CommonClientResponse();
                    commonClientResponse.setResponse( clazz1.newInstance() );
                    return commonClientResponse;
                };
            };
        }
    }

}

but it not even compile. Do you have any ideas of how i can begin implementing 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-25T23:32:21+00:00Added an answer on May 25, 2026 at 11:32 pm

    As you are effectively trying to create an anonymous class whose type is unknown at runtime, have you considered invoking the compiler at runtime? I haven’t used it much myself, but it may be worth investigating. You can invoke it by using

    JavaCompiler compiler = javax.tools.ToolProvider.getSystemJavaCompiler();
    

    Note that this will only work if the application is run on a system where JDK is install, as JRE (does not include javac).

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

Sidebar

Related Questions

Whilst refactoring some legacy C++ code I found that I could potentially remove some
Whilst refactoring some code I came across some getter methods that returns a std::string.
We’ve found that the unit tests we’ve written for our C#/C++ code have really
While refactoring some C# classes, I've run into classes that implement IDisposable. Without thinking,
Whilst refactoring some old code I realised that a particular header file was full
While refactoring some old code I have stripped out a number of public methods
I'm refactoring some PHP code and discovered that certain nested combinations of if ()
I am refactoring some MFC code that is littered with ASSERT statements, and in
I recently did some refactoring of my mvc application and realized that there are
Recently I have been refactoring some of my C# code and I found a

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.