Is there a nice way to reset the state of static Shadows?

606 views
Skip to first unread message

James 'zofrex' Sanderson

unread,
May 8, 2014, 11:08:18 AM5/8/14
to robol...@googlegroups.com
I've written a custom ShadowDateFormat that lets me change some of the static methods on DateFormat, such as #is24HourFormat. As these things are static, they persist from test to test, so at the moment I have quite a lot of code in my @Before method to reset these properties between tests.

Is there a nicer way to achieve this? In PowerMock I think you can do it by putting the @PrepareForTest on each test method rather than on the class, but putting Robolectric's @Config on methods instead of the class did not have the same effect, and @RunWith can only apply to classes.

zofrex

Erich Douglass

unread,
May 8, 2014, 11:12:14 PM5/8/14
to robol...@googlegroups.com
The shadows in Robolectric that have static state all have a reset()
method that's called before each test is run. There's no magic - the
test runner calls reset on each shadow that needs it. It's clunky and
we've never found a better solution.

I'd be interested to hear what you end up doing.
> --
> You received this message because you are subscribed to the Google Groups
> "Robolectric" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to robolectric...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jonathan Gerrish

unread,
May 9, 2014, 2:47:41 AM5/9/14
to robol...@googlegroups.com

It would be sweet if the framework could automatically call reset() on any custom shadow that was added.

James 'zofrex' Sanderson

unread,
May 9, 2014, 7:08:33 AM5/9/14
to robol...@googlegroups.com
I didn't know about the reset methods on Robolectric's shadows, but that's exactly what I ended up implementing for mine. Of course, I have to remember to call that in the @Before of every test suite that uses it.

It would be really nice if this mechanism in Robolectric was extensible. What if instead of calling the method named reset() it looked for static methods on registered shadows with a @Reset annotation? (as per effective Java 2nd edition item 35: Prefer annotations on naming patterns) Then custom shadows could add a @Reset-annotated method and have that automatically called for them by Robolectric.

There is another way you could do it which would be less error-prone (in the sense of you could make a mistake in the reset() method), and I think is what Powermock does: You could use a custom classloader to unload and reload the static class between tests, which would guarantee it was in the initial state. I assume that would be slower and more complicated?

zofrex

Erich Douglass

unread,
May 10, 2014, 1:05:05 PM5/10/14
to robol...@googlegroups.com
There are some really good ideas here. I've opened issue #1072 to see
if we can make it easier to do this with custom shadows (and built-in
shadows).
Reply all
Reply to author
Forward
0 new messages