Search code examples
junitmockitopowermockpowermockito

How to capture constructor arguments using PowerMockito


class A {
   public B getB(){
      // somehow get argType1 and argType2
      return new B(argType1, argType2);
   }
}

class B{
   public B(Type1 t1, Type2 t2){
     // something
   }
}

I want to Test A, and verify that constructor of B is getting called for expected values of argType1 and argType2.

How can i do this using PowerMockito?

Is there a way to pass argumentCaptor like this:

whenNew(B.class).withArguments(argType1Captor, argType2Captor).thenReturn(somemock);

if this do this, argType1Captor gets both the values


Solution

  • I solved it by doing this

    PowerMockito,verifyNew(B.class).withArgument(expectedArgType1, expectedArgType2)