3
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

bumble_bee_tuna
  • 3,533
  • 7
  • 43
  • 83
Dhrumil Upadhyaya
  • 1,014
  • 2
  • 10
  • 14

2 Answers2

5

I solved it by doing this

PowerMockito,verifyNew(B.class).withArgument(expectedArgType1, expectedArgType2)
Dhrumil Upadhyaya
  • 1,014
  • 2
  • 10
  • 14
4

Dhrumil Upadhyaya, your answer (admittedly the only one offered) doesn't solve the question you asked! I think you might want to do something like:

public class MyTest {
   private ArgType argument;

   @Before
   public void setUp() throws Exception {
       MyClass c = mock(MyClass.class);

       whenNew(MyClass.class).withAnyArguments()
                             .then((Answer<MyClass>) invocationOnMock -> {
                argument = (ArgType) invocationOnMock.getArguments()[0];
                return c;
            } 
        );
    }
}

It's not an elegant solution but it will capture the argument(s) passed to the constructor. The advantage over your solution is that if argument is a DTO then you can inspect it to see if it contains the values you were expecting.

Stormcloud
  • 2,065
  • 2
  • 21
  • 41