51

Just started to use Robolectric and it seems to be pretty much what I need. However, I've hit a bit of a roadblock with regards to the use of SharedPreferences.

I have two tests cases

  1. Activity expects a new/empty sharedPreferences

  2. Activity expects sharedPreferences with some data in it already

For Test Case 1, the tests are passing as expected, so all good :)

However, for Test Case 2 I can't seem to figure out a nice way to provide Robolectric with some fake data, so the Activity is able to access this fake data.

It feels like a very common use case, but I can't seem to figure out how to do it!

e4c5
  • 52,766
  • 11
  • 101
  • 134
pyko
  • 3,399
  • 5
  • 26
  • 31

4 Answers4

61

Found out how - seems so obvious now!

For those who are interested, you just get the sharedPreferences, and populate it with the required data.

SharedPreferences sharedPreferences = ShadowPreferenceManager.getDefaultSharedPreferences(Robolectric.application.getApplicationContext());
sharedPreferences.edit().putString("testId", "12345").commit();

If you have a custom SharedPreferences, you should be able to do this (haven't really tested properly, but should also work)

SharedPreferences sharedPreferences = Robolectric.application.getSharedPreferences("you_custom_pref_name", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("testId", "12345").commit();

Hope this has helped someone :)

pyko
  • 3,399
  • 5
  • 26
  • 31
  • 1
    +1, this helped me alot! (the second one does work perfect). I'm using this to avoid kicking off async tasks in test mode by setting an "is test mode" boolean on the shared prefs. – Jimmy May 23 '12 at 18:40
  • tip: in your code under test that calls getSharedPreferences, be sure you're using the application context, otherwise this won't work. – Andy Dennie Oct 16 '14 at 22:55
  • 1
    @Jimmy i'd rather accomplish this by using custom build config fields see: http://michiganlabs.com/string-constants-generated-gradle-build-configurations – martyglaubitz Jul 10 '15 at 12:16
  • 2
    I think this should be `PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application)` – sargas Apr 03 '17 at 00:26
  • Roboletric.application does not exist since 3.0, now it is RuntimeEnvironment.application check migrating to 3.0, major changes, first line http://robolectric.org/migrating/ – Mauricio Rodriguez Jun 01 '22 at 19:40
22

The accepted answer which I have up voted is right of course. Things have changed slightly if you are using Robolectric 3

 SharedPreferences sharedPreferences =
     RuntimeEnvironment.application.getSharedPreferences(
         "you_custom_pref_name", Context.MODE_PRIVATE);

You can then add a preference as usual

 sharedPreferences.edit().putBoolean(
    activity.getString(R.string.pref_somepref), true).commit();
e4c5
  • 52,766
  • 11
  • 101
  • 134
8

Robolectric 3.1 SNAPSHOT solution that works for me... and may work for you

    Context context = RuntimeEnvironment.application.getApplicationContext();
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    sharedPreferences.edit().putBoolean("useOnlyOnWifi", false).commit();

I use this snippet of code for only testing on wifi

Evan Parsons
  • 1,139
  • 20
  • 31
5

robolectric:4.0.2 use val context = ApplicationProvider.getApplicationContext<YourApplication>() PreferenceManager.getDefaultSharedPreferences(context)

shuabing
  • 679
  • 11
  • 28