5

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

Rob McFeely
  • 2,823
  • 8
  • 33
  • 50

1 Answers1

10

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.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • Thanks Jeff. The interesting (and somewhat counter-intuitive) thing here is that unlike setting up the mock/stub whcih goes before the testing of the logic, the code to setup the capture goes afterwards. – Rob McFeely Sep 12 '14 at 10:27
  • 1
    Full code please? I'm confused by the order of your method calls. I used your code exactly as is and it failed. It told me the method wasn't invoked. – Stevers Oct 23 '18 at 19:05
  • @Stevers I'm not quite sure what you mean by "Full code please"; in absence of a full example in the question, are you asking me to write an entire example setup and then an entire test for the example setup? I'm not sure I could do better than the documentation that already exists (to which I've updated the link). If you have an example that isn't working, that already exists as a MCVE, it sounds like an excellent case for a separate question that links to this one. – Jeff Bowman Oct 23 '18 at 19:22