0

When unit testing shared preference, is the value get refresh every test?

public static void putString(Context context, String key, String val) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, val);
        editor.apply();
    }
Alvin
  • 8,219
  • 25
  • 96
  • 177

2 Answers2

1

Once you run your function it will save the val under the key which will be saved for every other instance. If you want to be absolutely be sure of this, you can read the value using another function (and maybe a different instance) usingPreferenceManager.getDefaultSharedPreferences(context.getString(key, "default"));

Alternatively, like many have said, you can manually check the default SharedPreferences file located in /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml inside your app data folder, on the device you are testing on.

daedsidog
  • 1,732
  • 2
  • 17
  • 36
1

The shared preference does not get cleared until you clear the editor. Make sure when you are testing the screen again, the editor is not getting cleared. Only then the preference may get cleared out.

To access the shared.xml folder, you can follow this link

How can I view the shared preferences file using Android Studio?