title: PowerMock whenNew without @PrepareForTest?
body: <p>This is my situation, I have 2 very simple classes:</p>
  
  <pre><code>public class B {
  
      public void doSomething(){
          System.out.println("doSomething B reached");
      }
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>public class A {
  
  public void doSomething(){
      B b = new B();
      b.doSomething();
      System.out.println("doSomething A reached");
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I want to test method doSomething of class A with Mockito. Therefor, I want to mock an instance of class B and give this to A when it is instantiating class B. I don't want b.doSomething() to be reached at all, for isolation reasons. </p>
  
  <p>I know I can reach this behaviour by creating the following unittest:</p>
  
  <pre><code>@RunWith(PowerMockRunner.class)
  public class TestA {
  
      @Test
      @PrepareForTest(A.class)
      public void testDoSomethingOfA() throws Exception{
          A a = PowerMockito.spy(new A());
          B b = PowerMockito.mock(B.class);
          PowerMockito.whenNew(B.class).withNoArguments().thenReturn(b);
          a.doSomething();
      }
  }
  </code></pre>
  
  <p>which results in output:</p>
  
  <pre><code>doSomething A reached
  </code></pre>
  
  <p>So this work! However, my problem now is that we use the Jococo plugin for test coverage. Jococo doesn't cover code tested with the @PrepareForTest(A.class) statement. And my company values accurate code testing coverage.</p>
  
  <p>My question: Is there another way around to give A an instantiation of B without having to use the @PrepareForTest statement? </p>
  
  <p>Many thanks in advance!</p>