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
Activity expects a new/empty sharedPreferences
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!
Found out how - seems so obvious now!
For those who are interested, you just get the sharedPreferences, and populate it with the required data.
If you have a custom SharedPreferences, you should be able to do this (haven't really tested properly, but should also work)
Hope this has helped someone :)
The accepted answer which I have up voted is right of course. Things have changed slightly if you are using Robolectric 3
You can then add a preference as usual
robolectric:4.0.2
useval context = ApplicationProvider.getApplicationContext<YourApplication>() PreferenceManager.getDefaultSharedPreferences(context)
Robolectric 3.1 SNAPSHOT solution that works for me... and may work for you
I use this snippet of code for only testing on wifi