title: How should I test private methods in Java?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong>
    <a href="https://stackoverflow.com/questions/34571/whats-the-best-way-of-unit-testing-private-methods">What&rsquo;s the best way of unit testing private methods?</a></p>
  </blockquote>
  
  
  
  <p>I am a beginner programmer, and I don't know how to write an application that will be well structured for unit testing. I want to write applications with the ability to afterwards add effective unit tests.</p>
  
  <p>The problem is with <code>private</code> methods - they can't be testing with outside of their classes.</p>
  
  <p>Should I solve this problem by changing all methods that are <code>private</code> to <code>protected</code>, and let the test class extend source class? Or is there a better solution?</p>
  
  <p>My solution (private splitLetters => protected splitLetters) would work like this:</p>
  
  <p><strong>Source class:</strong></p>
  
  <pre><code>class MyClass{
    protected splitLetters(int num){
      return num+2;
    }
  }
  </code></pre>
  
  <p><strong>Test class:</strong></p>
  
  <pre><code>class Test_MyClass extend MyClass{
    public splitLettersTest(){
    for(int i=0;i&lt;100;i++){
      System.println(parent.splitLetters(i));
    }
   }
  }
  </code></pre>
  
  <p>Solutions:</p>
  
  <ol>
  <li><p><strong>Not testing private methods</strong> - Sometimes a private method is doing very complicated tasks that should be tested very well, and we don't want that user will have access to this methods. Soon the solution is changing private methods to protected.</p></li>
  <li><p><strong>Nested class way to test</strong> - problematic because QA make changes in source code</p></li>
  <li><p><strong>Reflection</strong> - If this makes it possible to call private methods, it looks like a great solution <a href="http://www.artima.com/suiterunner/private3.html" rel="noreferrer">http://www.artima.com/suiterunner/private3.html</a>
  (I should learn more to understand reflection. I don't understand how reflections do not break all the idea of having public and private methods if we can call private methods from another class.)</p></li>
  <li><p><strong>Not define private methods</strong> (as I showed in my solution) - problematic because sometimes we have to define a private method.</p></li>
  </ol>