title: PowerMockito mock a generic constructor
body: <p>I have a class that is declared like this:</p>
  
  <pre><code>public class GenericFoo&lt;T&gt;
  {
      private final Class&lt;T&gt; _c;
      public GenericFoo(Class&lt;T&gt; c)
      {
          this._c = c; 
      }
      /*
      ...
      rest is unimportant
      */
  }
  </code></pre>
  
  <p>I am writing a unit test for a class, call it <code>SomeBar</code> which creates and makes use of instances of <code>GenericFoo</code>. For this test I need the constructor to always return a specific spy of <code>GenericFoo</code>.</p>
  
  <pre><code>    MyWebServiceResponse webServiceResponseObject = setUpTestResponseObject(1,2,3);
  
      GenericFoo&lt;MyWebServiceResponse&gt; testFoo = PowerMockito.spy(new GenericFoo&lt;MyWebServiceResponse&gt;(MyWebServiceResponse.class));
  
      PowerMockito.when(testFoo.processResponseObject(webServiceResponseObject))
              .thenAnswer(new Answer&lt;String&gt;()
              {
                  @Override
                  public String answer(InvocationOnMock invocation) throws Throwable
                  {
                      /* details unimportant */
                  }
              });
  </code></pre>
  
  <p>To ensure that <code>testFoo</code> is returned when <code>SomeBar</code> creates an instance of <code>GenericFoo&lt;MyWebServiceResponse&gt;</code>, I've tried this:</p>
  
  <pre><code>PowerMockito.whenNew(com.myCompany.GenericFoo.class)
                  .withParameterTypes(MyWebServiceResponse.class)
                  .withArguments(MyWebServiceResponse.class)
                  .thenReturn(testFoo);
  </code></pre>
  
  <p>This causes PowerMock to produce the error:</p>
  
  <pre>
  org.powermock.reflect.exceptions.ConstructorNotFoundException: Failed to lookup constructor with parameter types [ com.myCompany.MyWebServiceResponse ] in class com.myCompany.GenericFoo.
  ...[stack trace snipped]
  Caused by: java.lang.NoSuchMethodException: com.myCompany.GenericFoo.(com.myCompany.MyWebServiceResponse)
  </pre>
  
  <p>Is there any way to get a generic constructor to return a specific object in this sort of situation?</p>