title: how can I stub the private method with reflection and then say the test code whenever it is called return `stubbed <X>` object?
body: <p>To test below sample source code, I have written a bunch of code using Java Reflection. However, I could not manage to run, so I have deleted.</p>
  
  <p>What I aim to do is basically reflect the <code>bar()</code> method of <code>CLASSZ</code>, and stub it so that whenever it is called in <code>foo()</code>, it will return stubbed instance of <code>Reader</code>. This stubbed instance then will send to <code>gar()</code> as a parameter.</p>
  
  <p>At the end of the test code, I was planned to call <code>foo()</code> method and check return value of it with another <code>String</code>. </p>
  
  <p>My question is "how can I stub the private method with reflection and then say the test code whenever it is called return stubbed <code>Reader</code> object?".</p>
  
  <pre><code>class CLASSZ{
      public String foo(){
          Reader reader = bar();
          String text = gar(reader);
          return text;
      }
  
      private Reader bar(){
         // do some operation related with Extractor; then create instance of Reader
         return new Reader(/*extractor related argument*/);
      }
  
      private String gar(Reader reader){
          // do something and return String
      }
  }
  </code></pre>