Verifying a static method is never called

5,475 views
Skip to first unread message

CraigA

unread,
Nov 29, 2011, 6:10:12 PM11/29/11
to PowerMock
I'm hitting a problem trying to verify that a static method is never
called. A non-static method isn't a problem of course. Something like
this will do it:

MyObj myObj = createMock(MyObj.class);

// . . . .

verify (myObj, never()).someMethod();

But what if all the methods in a class are static? Do you treat that
the same as a "normal" class? Or is it done separately if you do a
mockStatic() on the class?

mockStatic(MyStaticObject.class);

// . . . .

verify (MyStaticObject.someMethod(), never());

??? This must not be the correct approach as it's not working for
me. How does the syntax differ?

Thanks.

Oh, just as a postscript: Is there any way to verify that an exception
was thrown and caught in the tested code?

Johan Haleby

unread,
Nov 30, 2011, 10:37:07 AM11/30/11
to powe...@googlegroups.com
Hi, 

The documentation says that you should do something like this:

verifyStatic(never());
MyStaticObject.someMethod();

Hope that helps,
/Johan


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


CraigA

unread,
Nov 30, 2011, 12:17:30 PM11/30/11
to PowerMock
That's very useful. I was trying to find something like that and must
not have had the right key words in my search.

I find that Eclipse is redlining the verifyStatic(), so I assume that
there's something I'm missing in the imports. What's the proper import
statement to bring this in?

Thanks.

Craig

On Nov 30, 7:37 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> The documentation<http://code.google.com/p/powermock/wiki/MockitoUsage13#A_full_example...>says

Johan Haleby

unread,
Nov 30, 2011, 12:27:20 PM11/30/11
to powe...@googlegroups.com
It's located in the PowerMockito class, i.e. PowerMockito.verifyStatic();

/Johan

On 11/30/2011 06:17 PM, CraigA wrote

CraigA

unread,
Dec 5, 2011, 5:19:04 PM12/5/11
to PowerMock
Bear with me just a little longer, if you don't mind.
I tried the approach you mentioned:
mockStatic(MyObj.class);// . . . . verifyStatic(never());MyObj.someMethod();
but it chokes every time complaining about an unexpected call to
someMethod(). I traced through it with the debugger and it was never
called. It seems that it is treating the someMethod() as an actual
call, and then complaining that it was unexpected. I don't see how the
above should work.
Thanks,
Craig

Johan Haleby

unread,
Dec 6, 2011, 2:18:02 AM12/6/11
to powe...@googlegroups.com
Hmm it could of course be a bug but it ought to work :)

Check out the PowerMock testcase for static methods in trunk for examples.

Regards,
/Johan
Reply all
Reply to author
Forward
0 new messages