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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:33:08+00:00 2026-06-05T21:33:08+00:00

Let’s suppose I’ve to test this Java ClassA that depends on the difficult to

  • 0

Let’s suppose I’ve to test this Java ClassA that depends on the difficult to instantiate ClassB:

public class ClassA
{
    public ClassA()
    {
        String configFile = "config_file.xml";

        // I have to pass configFile to instantiate ClassB.
        // And for example if configFile does not exists in the testing machine?
        // Wouldn't it be easier to have an empty constructor for classB to test ClassA?
        ClassB classB = new ClassB(configFile);
    }

    // ...
}

ClassB:

class ClassB
{
    ClassB(String configFile)
    {
        // Set up configs.
    }

    // ...
}

Is it bad practice to create an empty constructor inside classB only for testing purposes?
Or is it better to rewrite a simplified mock ClassB for that?

  • 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-05T21:33:10+00:00Added an answer on June 5, 2026 at 9:33 pm

    It seems like yours would be a suitable scenario for IoC and Dependency Injection, using something like Spring or Guice.

    That way ClassA would just declare that it “needs” an instance of ClassB, and your IoC container would wire up and construct the required dependencies.

    However, if you don’t want to get a framework in the way, you can solve it by extracting an interface contract out of ClassB, and have ClassA depend on that, and receive a implementation to that contract in its constructor.

    For example:

    interface SomeContract {
        void someBehavior();
    }
    

    Have ClassB implement it:

    class ClassB implements SomeContract {
    {
        ClassB(String configFile)
        {
            // Set up configs.
        }
    
        void someBehavior() {
            // ...
        }
    }
    

    And use it, like:

    public class ClassA
    {
        private SomeContract implementation;
        public ClassA(SomeContract implementation)
        {
            this.implementation = implementation;
        }
    
        // ...
    }
    

    That way, in your unit test, you can pass an instance you determine in your test, such as a Mock, and you can test accordingly, without having to modify your code for ClassB.

    The bottom line is, that you should try to avoid creating additional constructors and such solely for testing purposes.

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

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's suppose I have this piece of code. foreach(string value in myList) { string
Let's imagine I have a Java class of the type: public class MyClass {
Let's say I need to provide a method with a java.lang.Class object that represents
Let's say I have a class like this : public class MyClass { public
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's suppose I have an XML file like this: <?xml version=1.0 encoding=ISO-8859-1?> <MIDIFile> <Event>
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say that I have a set of relations that looks like this: relations

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.