How to mock PreferenceManager in Android?

Viewed 7796

I've written a class that is using Context, a third party library and SharedPreferences from PreferenceManager.

It's possible to mock Context, the third party library can be mocked using some mocking framework, but what to do with PreferenceManager?

I have two methods:

public void saveString(ThirdPartyObject obj) {
    SharedPreferences appPreferences = 
        PreferenceManager.getDefaultSharedPreferences(mContext);
    SharedPreferences.Editor editor = appPreferences.edit();
    editor.putString(mContext.getString(
        R.string.preferences_string_name), obj.getString());
    editor.commit();
}

and corresponding, that loads preferences.

2 Answers
Related