title: Unit Test Best Practices: Empty Constructor or Mock Object?
body: <p>Let's suppose I've to test this Java <code>ClassA</code> that depends on the difficult to instantiate <code>ClassB</code>:</p>
  
  <pre><code>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);
      }
  
      // ...
  }
  </code></pre>
  
  <p><code>ClassB</code>:</p>
  
  <pre><code>class ClassB
  {
      ClassB(String configFile)
      {
          // Set up configs.
      }
  
      // ...
  }
  </code></pre>
  
  <p>Is it bad practice to create an empty constructor inside <code>classB</code> only for testing purposes?
  Or is it better to rewrite a simplified mock <code>ClassB</code> for that?</p>