Capturing arguments in mocked static calls?

11,733 views
Skip to first unread message

KARR, DAVID

unread,
May 17, 2013, 6:20:39 PM5/17/13
to powe...@googlegroups.com
I need to capture an argument sent to a static method in a mocked object. I actually think I've got this working, but it seems so odd (even odder than normal Mockito code seems), I'd just like to make sure this is doing what I think it's doing.

In class FooTest, I have a @PrepareForTest that includes "Bar.class", and in my @Before method I have a "PowerMockito.mockStatic(Bar.class)" statement.

I've defined an ArgumentCaptor instance variable with the @Captor annotation.

In my test method, AFTER it calls the code under test, I have the following:

PowerMockito.verifyStatic();
Bar.staticMethod(captor.capture());

<Type> value = captor.getValue();
assertThat(value).isEqualTo(...);

Is this the correct sequence of events?

Johan Haleby

unread,
May 21, 2013, 5:21:59 AM5/21/13
to powe...@googlegroups.com
Well you could use it like this:

mockStatic(StaticService.class);
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);

StaticService.say("something");

verifyStatic();
StaticService.say(captor.capture());

assertEquals("something", captor.getValue());

Regards
/Johan



--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



Arpit Krishna

unread,
Mar 29, 2018, 9:40:49 AM3/29/18
to PowerMock
This is indeed correct. Thanks. Read more at: https://github.com/powermock/powermock/wiki/Mockito
Reply all
Reply to author
Forward
0 new messages