title: Alternatives to mock an static method
body: <p>I am developing a desktop application, I want to test the method <code>foo</code> of <code>MyClass</code> and I have the following scenario:<br>
  MyClass:</p>
  
  <pre><code>import package.MainWindow;
  
  public class MyClass{
  
      public int foo(){
          //some logic
          // . . .
          boolean result = getResult();
          if (result) {
              MainWindow.printInMain("Success");
              return 1;
          } else {
              MainWindow.printInMain("Fail: " + getCommentsAsString());
              return 2;
          }
      }
  }
  </code></pre>
  
  <p>Then, I have <code>MainWindow</code> with its static method <code>printInMain</code>:  </p>
  
  <pre><code>public class MainWindow{
  
      private static JTextArea jTextArea;
  
      MainWindow(){
          jTextArea = new JTextArea();
      }
  
      public static void printInMain(String string) {
          jTextArea.append(string + "\n");
          try {
             jTextArea.setCaretPosition(jTextArea.
               getLineStartOffset(jTextArea.getLineCount() - 1));
          } catch (BadLocationException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p><code>MainWindow</code> is the interface of my program, and jTextArea is the swing component that is printing the information that I send from <code>MyClass.foo()</code>.<br>
  Now I am testing <code>foo()</code> with jUnit, and I don't need the graphic user interface to test it. The test is pretty straight forward, but I get a nullPointer exception when calling the printing method because <code>MainWindow</code> hasn´t been instantiated so <code>jTextArea</code> is null.<br>
  My question is, is there any clean way of avoiding this error of being thrown?<br>
  I am using mockito and as far as I know, <a href="https://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito">static methods cannot be mocked</a>.<br>
  In some other cases, I could have done something like this:</p>
  
  <pre><code>MainWindow mainWindow = Mockito.mock(MainWindow.class);
  </code></pre>
  
  <p>But <code>MainWindow</code> is not being set in <code>MyClass</code>, so this is not very useful either.<br>
  Finally I came up with a workaround that does what I want, but it's not very clean. Basically I have declared a static boolean flag <code>printText</code> and set it as <code>false</code> at the very beginning of <code>MainWindow</code>, and I am setting it as true in the class constructor. Then, in <code>printInMain</code> I only print if this flag is true, which will only happen if this class has been previously instantiated.<br>
  This approach works but I was wondering whether there is a better solution than this, maybe using Mockito or some other mocking or testing techniques, as I am not very familiar with them.<br>
  I don't think this is a very good option because I am adding some code in the application just to make the test work, and the test should adapt to my code, not the code to my test. </p>