Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capture parameters passes to stub in powermockito

How can I capture (for assertion purposes) the parmeters passed to a static stub method call?

The methodBeingStubbed looks like this...

public class SomeStaticClass{
protected static String methodBeingStubbed(Properties props){
...

I am stubbing the method call because it i need to verify that it gets called...

PowerMockito.stub(PowerMockito.method(SomeStaticClass.class, "methodBeingStubbed")).toReturn(null);
PowerMockito.verifyStatic();

But I now also want to know what properties were passed to this "methodBeingStubbed" and assert it is as expected

like image 814
Rob McFeely Avatar asked Jan 10 '23 20:01

Rob McFeely


1 Answers

After the call to verifyStatic, you'll need to actually call the method you're trying to verify, as in the documentation here:

PowerMockito.verifyStatic(Static.class);
Static.thirdStaticMethod(Mockito.anyInt());

At that point you can use Mockito argument captors, as demonstrated (but not tested):

ArgumentCaptor<Properties> propertiesCaptor =
    ArgumentCaptor.forClass(Properties.class);

PowerMockito.verifyStatic(SomeStaticClass.class);
SomeStaticClass.methodBeingStubbed(propertiesCaptor.capture());

Properties passedInValue = propertiesCaptor.getValue();

If you're used to @Mock annotations, or you need to capture a generic (as in List<String>), you may also be interested in using the @Captor annotation instead.

like image 78
Jeff Bowman Avatar answered Jan 16 '23 22:01

Jeff Bowman