title: powermockito.when().thenReturn() - calls mocked function
body: <p>Using PowerMockito to setup a test containing a private method ala <a href="https://code.google.com/p/powermock/wiki/MockitoUsage13#A_full_example_of_partial_mocking_of_a_private_method" rel="nofollow">this</a>.</p>
  
  <p>Class:</p>
  
  <pre><code>public class SomeMod {
      public void somefunc() {
          Logger.debug("somefunc");
          ...
          privFunction(param);
          ...
      }
  
      private void privFunction(param) {
          Logger.debug("privFunction");
      }
  }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest({SomeMod.class})
  public class somemod_test {
      ...
      @Test public void test0() {
          SomeMod_spy somemod = spy(new SomeMod());
          PowerMockito.when(somemod, "privFunction", "param").thenReturn(someMockedValue);
  
          ...
          somemod.somefunc();
      }
  
  }
  </code></pre>
  
  <p>When I run this and set breakpoints on both Logger.debug statements Im seeing the <code>privFunction</code> get hit before the <code>somefunc</code>.  If I comment out the call to <code>somemod.somefunc()</code> in the test, the <code>privFunction</code> is <em>still</em> getting hit.</p>
  
  <p>What am I doing wrong?</p>
  
  <hr>
  
  <h2>Edit:</h2>
  
  <p>I used <code>PowerMockito.when().thenReturn()</code> instead of the recommeded <code>PowerMockito.doReturn().when()</code> syntax as the former throws NPE and <strong>UnfinishedStubbingException</strong>. This suggests that my test environment isn't setup properly. Not sure where to look to fix this.</p>