how to create a shadow or a mock for a static class ?

3,749 views
Skip to first unread message

Gal Ben-Haim

unread,
Aug 22, 2012, 4:01:09 AM8/22/12
to robol...@googlegroups.com
I have a static class that access a HTTP API which I want to mock in order to properly unit test all units that use that static class.

I tried using 'PowerMock' with 'Mockito' and 'PowerMockRule' according to this post - https://twitter.com/robolectric/status/24713827790622720, without any success
I also tried creating a shadow for the static method but it doesn't work unless I'm changing the package name of the static class to android.<something>, according to this - http://stackoverflow.com/questions/7250086/how-does-exactly-custom-shadow-objects-work-in-robolectric/7252694#7252694

is it possible to make `PowerMock` work after all for a situation like this ? is there another tool that will allow me to mock a static method in conjunction with Robolectric ?
or how can I create a shadow for this static method without changing the package name (is it possible at all without changing Robolectric source code) ?

Aaron VonderHaar

unread,
Aug 24, 2012, 2:54:20 AM8/24/12
to robol...@googlegroups.com
Shadows rely on there being an instance of the object being shadowed,
so afiak there's no way to shadow static methods.

I'm not familiar with PowerMock, and I haven't ever tried mocking
static methods with Mockito, but I don't see any reason why those
shouldn't work as they would in a non-android project, since
robolectric tests run in the normal JVM.

(Oh, and I'm assuming by "static class" you mean a class with only
static methods that you never instantiate, as opposed to a "static
inner class"?)

--Aaron V.
--
--Aaron V.
[ http://github.com/avh4 ]

Gal Ben-Haim

unread,
Aug 24, 2012, 3:30:58 AM8/24/12
to robol...@googlegroups.com

I ended up refactoring the code to use an interface, so I can inject a mock implementation with Mockito.

Tyler Schultz

unread,
Aug 24, 2012, 5:34:37 PM8/24/12
to robol...@googlegroups.com, robol...@googlegroups.com
FYI - static methods are in fact 'Shadow-able'. There are many examples in the shadow collection included with Robolectric. I'm not in front of the source right now, so I'm not able to provide an example.


--Tyler

Gal Ben-Haim

unread,
Aug 25, 2012, 1:36:52 AM8/25/12
to robol...@googlegroups.com

I was able to create a shadow for the static class,  but I had to change it's package name to android.*

I wish it was possible without doing so.

Aaron VonderHaar

unread,
Sep 4, 2012, 1:09:55 AM9/4/12
to robol...@googlegroups.com
Glad it works for static methods!

You can bind shadows in other packages if you use
addClassOrPackageToInstrument(String). But if you call it too late
(after Robolectric does the ClassLoader magic), then it won't work.
The best place I've found to do it is to subclass
RobolectricTestRunner and do it in the constructor.

See https://gist.github.com/2306799

--Aaron V.
Reply all
Reply to author
Forward
0 new messages