title: Mocking method for all instances of class with PowerMockito
body: <p>
  I am trying to mock a method whatever the instance that calls this method is. As far as I could got reading this can't happen with Mockito and should be done with PowerMockito, but I can't figure out the way it should be done.
  I have</p>
  
  <pre class="lang-java prettyprint-override"><code>public class B{
      public void veryAnnoyingMethod(int i, short s){}
  }
  public class A{
      public void veryImportantMethod(){
          B newB = new B();
          newB.veryAnnoyingMethod(i,s);
          ...
      }
  }
  </code></pre>
  
  <p>I am trying to test the <code>veryImportantMethod()</code> and want to mock the <code>veryAnnoyingMethod()</code>.
  I don't want to mock the constructor as explained here: <a href="https://stackoverflow.com/questions/6520242/mocking-methods-of-local-scope-objects-with-mockito">Mocking methods of local scope objects with Mockito</a> because apart from the <code>veryAnnoyingMethod()</code> I need the <code>newB</code> fully functional.</p>
  
  <p>I have come up with something like:</p>
  
  <pre class="lang-java prettyprint-override"><code>B dummyB = PowerMockito.spy(new B());
  PowerMockito.doReturn(null).when(dummyB.veryAnnoyingMethod(Mockito.anyInt(), Mockito.anyShort()));
  </code></pre>
  
  <p>But it throws exception, and I don't think it is what I actually need.</p>
  
  <p>UPDATE</p>
  
  <p>After a bit more reading it seems I have to use <code>PowerMockito.stub</code>, or <code>PowerMockito.supress</code>. So I have tried the following:</p>
  
  <pre class="lang-java prettyprint-override"><code>PowerMockito.stub(PowerMockito.method(B.class, "veryAnnoyingMethod", int.class, short.class)).toReturn(null);
  </code></pre>
  
  <p>or</p>
  
  <pre class="lang-java prettyprint-override"><code>PowerMockito.supress(PowerMockito.method(B.class, "veryAnnoyingMethod", int.class, short.class));
  </code></pre>
  
  <p>But with either the method is still executed</p>