title: Powermock Mockito : the method I'm trying to stubb ends up being called
body: <p>I'm trying to test a <code>class A</code> which in turn autowires a <code>class B</code> :</p>
  
  <pre><code>public class A {
  
     @Autowired
     private B b;
  
     public int foo(int x, int y) {
         int z = b.bar(x, y, false);
         //do something with z
         return z;
     }
  }
  </code></pre>
  
  <p>I'm using junit together with <code>powermock</code> and <code>mockito</code> to try to test the method <code>foo</code> in <code>class A</code>.</p>
  
  <pre><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest(A.class)
  public class TestA {
  
       @Test
       private void testFoo() {
          B b = PowerMockito.mock(B.class);
          when(b.bar(2,3,false)).thenReturn(5);
          A a = PowerMockito.spy(new A());
          Whitebox.setInternalState(a, "b", b);
  
          int z = a.foo(2,3);
          Assert.assertEquals(10,z);
       }
  
  }
  </code></pre>
  
  <p>When i try to run the test I get a <code>NullPointerException</code> from within <code>class B</code>.
  After using the debugger, I found out that right after stubbing the <code>bar</code> method of the <code>B class</code>, the <code>bar</code> method gets called.
  The null pointer exception in this case is normal because the B class is not properly initialized.</p>
  
  <p>Can anyone explain why this is happening and what can I do about it.</p>