title: powermock mocking constructor via whennew() does not work with anonymous class
body: <p>I have a DummyResource class and a DummyTarget file, and a test class TestDummyResource as below, but the mocked object <code>DummyResource dr = mock(DummyResource.class)</code> only works when I call the constructor inside a normal class, when it's called in an anonymous class, it's calling the actual constructor instead of using the mocked object.</p>
  
  <p><strong>Versions:</strong></p>
  
  <p>powermock 1.4.12 mockito 1.9.0 junit 4.8.2</p>
  
  <p><strong>DummyTarget.java:</strong></p>
  
  <pre><code>import java.io.IOException;
  import java.io.OutputStream;
  
  import javax.ws.rs.WebApplicationException;
  import javax.ws.rs.core.StreamingOutput;
  
  
  public class DummyTarget {
      public StreamingOutput testMocking() {
          return new StreamingOutput() {
              @Override
              public void write(OutputStream arg0) throws IOException, WebApplicationException {
                  new DummyResource();
              }
          };
      }
  }
  </code></pre>
  
  <p><strong>DummyResource.java:</strong></p>
  
  <pre><code>package com.smin.dummy;
  
  public class DummyResource {
      public DummyResource() {
          System.out.println("mock failure");
      }
  }
  </code></pre>
  
  <p><strong>TestDummyResource.java:</strong></p>
  
  <pre><code>package com.smin.dummy;
  
  import static org.mockito.Mockito.mock;
  
  import java.io.IOException;
  
  import javax.ws.rs.WebApplicationException;
  import javax.ws.rs.core.StreamingOutput;
  
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.powermock.api.mockito.PowerMockito;
  import org.powermock.core.classloader.annotations.PrepareForTest;
  import org.powermock.modules.junit4.PowerMockRunner;
  
  @RunWith(PowerMockRunner.class)
  @PrepareForTest({DummyResource.class,DummyTarget.class})
  public class TestDummyResource  {
  
      @Before
      public void setUp() throws Exception {
          DummyResource dr = mock(DummyResource.class);
          PowerMockito.whenNew(DummyResource.class).withNoArguments().thenReturn(dr);
      }
  
      @Test
      public void testMocked() throws WebApplicationException, IOException {
          new DummyResource(); // it uses the mocked object dr here, 
                               //doesn't print "mock failure"
          StreamingOutput sop = new DummyTarget().testMocking();
          sop.write(null);     // it calls DummyResource's constructor,
                               // prints ""mock failure"" here
      }
  }
  </code></pre>