Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing test data for SharedPreferences for Robolectric

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!

like image 733
pyko Avatar asked Mar 17 '12 08:03

pyko


People also ask

How can I check my data usage on SharedPreferences?

Click on the package name for your application. After that click on the shared_prefs folder and inside that open the shared_prefs. xml file. Now you will get to see the data which we stored in our shared preferences from our application.

What is the method used to store and retrieve data on the SharedPreferences?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Which scenarios do I need SharedPreferences?

If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs. A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them.


2 Answers

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

like image 177
pyko Avatar answered Oct 02 '22 08:10

pyko


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(); 
like image 27
e4c5 Avatar answered Oct 02 '22 10:10

e4c5