Reputation: 1154
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
Upvotes: 3
Views: 5716
Reputation: 2297
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.
Upvotes: 4
Reputation: 1154
I solved it by doing this
PowerMockito,verifyNew(B.class).withArgument(expectedArgType1, expectedArgType2)
Upvotes: 5