title: Mocking private methods used in public method with PowerMock, Mockito, and TestNG
body: <p>I want to write a unit test for a class I have. This class has a public method, and inside the public method there are calls to private methods in the same class. I want to mock the calls to those private methods. The class is similar to this:</p>
  
  <pre><code>public class SomeClass {
      public int somePublicMethod(int num) {
          int num2 = somePrivateMethod1(num);
          int num3 = somePrivateMethod2(num);
  
          return num2 + num3;
      }
  
      private int somePrivateMethod1(int num) {
          return 2*num;
      }
  
      private int somePrivateMethod2(int num) {
          return 3*num;
      }
  }
  </code></pre>
  
  <p>For my unit test I am trying to use PowerMock with Mockito and TestNG. Here is my attempt at a test that tests somePublicMethod:</p>
  
  <pre><code>import static org.powermock.api.mockito.PowerMockito.doReturn;
  import static org.powermock.api.mockito.PowerMockito.spy;
  
  import org.powermock.core.classloader.annotations.PrepareForTest;
  import org.testng.Assert;
  import org.testng.annotations.Test;
  
  @PrepareForTest(SomeClass.class)
  public class SomeClassTest {
  
      @Test
      public void testSomePublicMethod() throws Exception {
          int num = 4;        
  
          SomeClass someClassSpy = spy(new SomeClass());
  
          doReturn(8).when(someClassSpy, "somePrivateMethod1", num);
          doReturn(12).when(someClassSpy, "somePrivateMethod2", num);
  
          int result = someClassSpy.somePublicMethod(num);
  
          Assert.assertEquals(result, 20);
      }
  }
  </code></pre>
  
  <p>When I run this test I get an exception and some hints:</p>
  
  <pre><code>FAILED: testSomePublicMethod
  org.mockito.exceptions.misusing.UnfinishedStubbingException: 
  Unfinished stubbing detected here:
  -&gt; at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:31)
  
  E.g. thenReturn() may be missing.
  Examples of correct stubbing:
      when(mock.isOk()).thenReturn(true);
      when(mock.isOk()).thenThrow(exception);
      doThrow(exception).when(mock).someVoidMethod();
  Hints:
   1. missing thenReturn()
   2. you are trying to stub a final method, you naughty developer!
  </code></pre>
  
  <p>I've looked at some examples online but I haven't found one that uses PowerMock with Mockito and TestNG specifically to do what I want. Can someone give me some pointers on what I could do differently?</p>