title: Mockito/PowerMockito mocking private factory method
body: <p>I want to use pattern 1 suggested in the following link:
  <a href="https://code.google.com/p/mockito/wiki/MockingObjectCreation" rel="nofollow">https://code.google.com/p/mockito/wiki/MockingObjectCreation</a>
   and have the following class:</p>
  
  <pre><code>public class MyClass {
    private AnyType anyObject;
    private Foo foo; // Foo is a thirdparty class
  
    public MyClass(AnyType anyObject) {
      //...
      foo = makeFoo();
    }
  
    private Foo makeFoo() {
      return new Foo();
    }
  }
  </code></pre>
  
  <p>I'm trying to make a test as follows:</p>
  
  <pre><code>@Test
  public void myTestMethod() {
    MyClass myClass = Mockito.spy(new MyClass());
  
    // now i want to do something like this:
    Foo mockFoo= Mockito.mock(Foo.class);
    // Mockito.doReturn(mockFoo).when(myClass).makeFoo());
  }
  </code></pre>
  
  <p>The problem is my factory method makeFoo is a private method, so I can't access it. I don't want to make it public just for the test. My test classes are not in the same package as my productive code either, so making it visible only for the package won't work.</p>
  
  <p>Update:
  Now i found another problem. Assumed that makeFoo() is public, 'mockFoo' will not be returned either, yet the real makeFoo() method is invoked. This happens since the invocation of makeFoo() (in the constructor of MyClass) is prior to the creation of mockFoo.</p>
  
  <p>Does anyone know how to solve this problem or am I doing something totally wrongly?</p>
  
  <p>Thanks you guys in advance for helping!!</p>