title: UnfinishedStubbingException when performing stubbing from a class other than the test class
body: <p>I'm using PowerMock to mock a java.net.Inet4Address (amongst other things) to return a particular IP address (<code>getHostAddress()</code>), as well as whether or not it's loopback (<code>isLoopbackAddress()</code>). I find that if I perform the actual stubbing (<code>PowerMock.doReturn(...).when(mock).methodToStub()</code>) anywhere other than from within the test class or an immediately inner class I get an UnfinishedStubbingException.</p>
  
  <p>The problem is most obvious if I try to perform two stubs. The first one passes without an exception, but the second one throws the exception because it thinks the first one was not finished. If I only perform the one stub then I see different errors depending on what I do after that, so it's definitely the first stub that's causing the problem.</p>
  
  <p>Below is some code that demonstrates the problem.</p>
  
  <p>TestClass.java</p>
  
  <pre class="lang-java prettyprint-override"><code>/* package, imports... */
  
  @RunWith(PowerMockRunner.class)
  @PrepareForTest({Inet4Address.class})
  public class TestClass {
  
      @Test
      public void test() {
          Inet4Address mocked = PowerMockito.mock(Inet4Address.class);
  
          // Option 1: Do it from within this class - WORKS
          doStubbing(mocked);
  
          // Option 2: Do it from an inner class - WORKS
          Inner.doStubbing(mocked);
  
          // Option 3: Do it from an inner class of the inner class - FAILS
          Inner.Deeper.doStubbing(mocked);
  
          // Option 4: Do it from an entirely different class - FAILS
          OtherClass.doStubbing(mocked);
      }
  
      private void doStubbing(Inet4Address mocked) {
          PowerMockito.doReturn(true).when(mocked).isLoopbackAddress();
          PowerMockito.doReturn("127.0.0.1").when(mocked).getHostAddress();
      }
  
      public static class Inner {
  
          static void doStubbing(Inet4Address mocked) {
              PowerMockito.doReturn(true).when(mocked).isLoopbackAddress();
              PowerMockito.doReturn("127.0.0.1").when(mocked).getHostAddress();
          }
  
          public static class Deeper {
  
              static void doStubbing(Inet4Address mocked) {
                  PowerMockito.doReturn(true).when(mocked).isLoopbackAddress();
                  PowerMockito.doReturn("127.0.0.1").when(mocked).getHostAddress();
              }
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>OtherClass.java</p>
  
  <pre class="lang-java prettyprint-override"><code>/* package, imports... */
  
  public class OtherClass {
  
      public static void doStubbing(Inet4Address mocked) {
          PowerMockito.doReturn(true).when(mocked).isLoopbackAddress();
          PowerMockito.doReturn("127.0.0.1").when(mocked).getHostAddress();
      }
  
  }
  </code></pre>
  
  <p>I've put the creation of the mock at the start, common to all scenarios. It makes no difference if the mock is created from within the same class where the stubbing is being done. I've also made the methods static for ease of reading; the behaviour is the same if the classes are instantiated first.</p>
  
  <p>I know there are workarounds so I can get my test working (perform the mocking right there in the test class, mock the InetAddress interface instead of the IPv4 implementation, etc) but I'd like to know <em>why</em> PowerMock is behaving in this way. I could <em>almost</em> understand it if it only worked from within the test class, but why does it work in an inner class as well?</p>