title: How do I completely mock out a class with PowerMockito?
body: <p>I'm reading this <a href="https://code.google.com/p/powermock/wiki/MockitoUsage13" rel="nofollow">documentation on PowerMockito</a> and it has two main examples:</p>
  
  <ol>
  <li>Mocking static methods</li>
  <li>Partially mocking a class</li>
  </ol>
  
  <p>but I want to know how to mock an entire class that's created with <code>new</code>.  <strong>I am looking for the PowerMockito version of Mockito's <code>mock</code> method</strong>.  This should be able to replace <code>new Foo()</code> in my production code with a Mockito <code>mock(Foo.class)</code>, somehow.  Here's what I've tried:</p>
  
  <pre><code>import com.PowerMockitoProduction;
  import org.apache.commons.httpclient.HttpClient;
  import org.apache.commons.httpclient.HttpMethod;
  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;
  
  import static org.mockito.Matchers.any;
  import static org.powermock.api.mockito.PowerMockito.when;
  
  
  @RunWith(PowerMockRunner.class)
  @PrepareForTest(PowerMockitoProduction.class)
  public class PowerMockitoTest {
  
      @Test(expected = UnsupportedOperationException.class)
      public void test() throws Exception {
  
          HttpClient mock = PowerMockito.mock(HttpClient.class);
          when(mock.executeMethod(any(HttpMethod.class))).thenThrow(UnsupportedOperationException.class);
  
          new PowerMockitoProduction().createClient();
  
      }
  }
  </code></pre>
  
  <p>This test fails with this:</p>
  
  <pre><code>java.lang.Exception: Unexpected exception, expected&lt;java.lang.UnsupportedOperationException&gt; but was&lt;java.lang.IllegalArgumentException&gt;
  </code></pre>
  
  <p>Here's my production code:</p>
  
  <pre><code>package com;
  
  import org.apache.commons.httpclient.HttpClient;
  
  import java.io.IOException;
  
  
  public class PowerMockitoProduction {
  
      public void createClient() throws IOException {
          HttpClient client = new HttpClient();
          client.executeMethod(null);
          System.out.println(client);
      }
  
  }
  </code></pre>
  
  <p>With my debugger, I can see that the <code>client</code> is not a mock, like I expected.</p>
  
  <p>I've also tried using:</p>
  
  <pre><code>Object mock = PowerMockito.whenNew(HttpClient.class).withNoArguments().getMock();
  </code></pre>
  
  <p>But for some reason, that returns a mock that is not completely constructed.  I've also tried this:</p>
  
  <pre><code>HttpClient mock = PowerMockito.whenNew(HttpClient.class).withNoArguments().thenReturn(mock(HttpClient.class)).getMock();
  </code></pre>
  
  <p>But that gives me a <code>ClassCastException</code> on that line.  So, what is the correct way to mock out a class completely with PowerMockito?</p>
  
  <p>Unlike this example implies, <strong>the reason I'm trying to mock out HttpClient is so that I can call <code>verify</code> it later.</strong></p>