Shadowing static methods in Android API

2,532 views
Skip to first unread message

Michael Portuesi

unread,
Nov 11, 2010, 2:42:10 PM11/11/10
to robolectric
My app code does the following:

public int prefsDefaultVolume() {
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);
return prefs.getInt( PREFS_KEY_DEFAULT_VOLUME, 50 );
}

I need to shadow the static method
PreferenceManager.getDefaultSharedPreferences, and have it return a
TestSharedPreferences. With my test case, I also must have set the
preferences in advance for getDefaultSharedPreferences() to return it.

How does Robolectric deal with shadowing static methods in the Android
API? I'm trying to solve this myself, but a pointer to a solution
would be helpful.

thanks,
Michael Portuesi

Tyler Schultz

unread,
Nov 11, 2010, 3:14:22 PM11/11/10
to robol...@googlegroups.com
Static methods are supported, just like a non-static method.

In your case you could do something this:

public class ShadowPreferenceManager {
  ...
  @Implements
  public static SharedPreferences getDefaultSharedPreferences(Context context) {
     return new TestSharedPreferences("__default__", MODE_PRIVATE);
  }
}


This will work because the backing map inside of TestSharedPreferences is static, and is reset between tests in TestHelper.

If you want to set preconditions, just use the PreferencesManager directly before you execute the app logic in your test.

--T$

Wadi Jalil Maluf

unread,
Feb 28, 2013, 4:31:16 PM2/28/13
to robol...@googlegroups.com
Hi Tyler, 
Trying to do as you suggested for the static Location.distance method. I found that there is a method called bindShadowClasses but seems to be that in the las version of Robolectric I can't find it(although it's in the javadoc). Probably I'm doing something wrong. 
Any help would be appreciated.
Thanks in advance,
Wadi

Tyler Schultz

unread,
Feb 28, 2013, 11:30:44 PM2/28/13
to robol...@googlegroups.com
I'm not aware of any static Location.distance method... I didn't see mention of any such method mentioned in the documentation:


Robolectric already provides a ShadowLocation class that should allow you to fake out distance measurements:

--Tyler


--
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/groups/opt_out.
 
 

Wadi

unread,
Mar 1, 2013, 8:05:28 AM3/1/13
to robol...@googlegroups.com
Tyler,
         The method is called distanceBetween. If you see at the docs you send me you will see that it's a static method. 
         I've been reading the forum and I saw that the suggestion is to use a mocking framework instead of shadowing classes so I'm going with that aproach(even when I know I'll have some dificulties mocking static methods).
Any comments are welcome,
Thanks for your time,
Wadi
Reply all
Reply to author
Forward
0 new messages