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!

4 Answers 4

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 :)

5
  • 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, 2012 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. Oct 16, 2014 at 22:55
  • 1
    @Jimmy i'd rather accomplish this by using custom build config fields see: michiganlabs.com/… Jul 10, 2015 at 12:16
  • 2
    I think this should be PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application)
    – sargas
    Apr 3, 2017 at 0:26
  • Roboletric.application does not exist since 3.0, now it is RuntimeEnvironment.application check migrating to 3.0, major changes, first line robolectric.org/migrating Jun 1, 2022 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();
1
  • I am getting cannot resolve RuntimeEnvironment error message
    – user7691120
    Jun 13, 2018 at 2:15
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

5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.