title: Mocking a parameterized constructor using powermockito
body: <p>Consider the code given below:</p>
  
  <pre><code>@Override
  public void A()  {
      objectA = objectB.B();
      objectA.C(someValue);
          objectC = new constructor(objectA,callback());
          //Rest of the code
      }
  
  }
  public Callback callback() {
      return new callback() {
          @Override
          public void someMethod(someArgument) {
              //some Code         
         }
      };
  }
  </code></pre>
  
  <p>I am trying to write a unit test case where:  </p>
  
  <ul>
  <li>the call <code>objectB.B()</code> has to be mocked</li>
  <li>the call to the constructor has to be mocked</li>
  </ul>
  
  <p>This is what I have done using Mockito and Powermockito:</p>
  
  <pre><code>@InjectMocks
  ClassBeingTested testObject;
  
  @Mock
  ClassB objectB;  
  
  @Mock
  ClassC objectC;    
  
  @Before()
  public void setup()  {
      when(objectB.B()).thenReturn(new ObjectA("someValue"));
  
      whenNew(objectC.class).withArguments(any(),any()).thenReturn(objectC);
  
  }
  @Test()
  public void testMethod() {
      testObject.A();
  
  }
  </code></pre>
  
  <p>The first mock successfully works but the second mock using <code>whenNew</code> fails with the following error:</p>
  
  <blockquote>
    <p>org.powermock.reflect.exceptions.ConstructorNotFoundException: No constructor found in class 'ClassC' with parameter types: [ null ]</p>
  </blockquote>
  
  <p>If I use <code>withArguments(objectA, callback())</code> where I have the implementation of callback in my test class, the actual constructor is called.</p>
  
  <p>I want to mock the constructor call and restrict the call to the actual constructor. How can I do that? </p>
  
  <blockquote>
    <p>I can not edit the code design since that is out of my scope.</p>
  </blockquote>