Providing test data for SharedPreferences for Robolectric

JavaAndroidAndroid TestingRobolectric

Java Problem Overview


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!

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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();

Solution 3 - Java

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

Solution 4 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpykoView Question on Stackoverflow
Solution 1 - JavapykoView Answer on Stackoverflow
Solution 2 - Javae4c5View Answer on Stackoverflow
Solution 3 - JavaEvan ParsonsView Answer on Stackoverflow
Solution 4 - JavashuabingView Answer on Stackoverflow