Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Unit Test : how to clear SharedPreferences

Tags:

I am trying to clear all SharedPreferences added during my tests. I already read some posts and the official documentation (SharedPreferences.Editor.clear()). But when I start my application after the unit tests were run, I still found test values.

So, in the AndroidTestCase.tearDown(), I make this :

public class PrivateStorageUtilsTest extends AndroidTestCase {  private static final String KEY_SP_PACKAGE = "PrivateStorageUtilsTest";      protected void setUp() throws Exception {         super.setUp();          // Clear everything in the SharedPreferences         SharedPreferences sharedPreferences = getContext()             .getSharedPreferences(KEY_SP_PACKAGE, Context.MODE_PRIVATE);         SharedPreferences.Editor editor = sharedPreferences.edit();         editor.clear();         editor.commit();     }      protected void tearDown() throws Exception {         // Clear everything in the SharedPreferences         SharedPreferences sharedPreferences = getContext().             getSharedPreferences(KEY_SP_PACKAGE, Context.MODE_PRIVATE);         SharedPreferences.Editor editor = sharedPreferences.edit();         editor.clear();         editor.commit();     }  } 

Every other questions I found on SO was about adding commit() after the clear(), which I already done here.

EDIT 1 Adding setUp() method

EDIT 2 Providing extended class

like image 898
mithrop Avatar asked Jul 22 '13 15:07

mithrop


People also ask

How do I clear all SharedPreferences in flutter?

// You can simply use clear() function with your variable it // will clear all the shared preferences. SharedPreferences preferences = await SharedPreferences. getInstance(); await preferences.

Does SharedPreferences persist after uninstall?

SharedPreferences is application specific, i.e. the data is lost on performing one of the following options: on uninstalling the application.

What do you call this method which deletes a single names preference?

Removing single preference: SharedPreferences settings = context.getSharedPreferences("PreferencesName", Context.MODE_PRIVATE); settings.edit().remove("KeyName").commit();


2 Answers

If you are using ActivityTestRule from Espresso, try this:

@Rule public ActivityTestRule<MainActivity> activityTestRule =     new ActivityTestRule<MainActivity>(MainActivity.class) {         @Override         protected void beforeActivityLaunched() {             clearSharedPrefs(InstrumentationRegistry.getTargetContext());             super.beforeActivityLaunched();         }     }; 

With a slightly modified version of stevo.mit's clearSharedPrefs:

private static final String KEY_SP_PACKAGE = "PrivateStorageUtilsTest";  /** * Clears everything in the SharedPreferences */ private void clearSharedPrefs(Context context) {     SharedPreferences prefs =          context.getSharedPreferences(KEY_SP_PACKAGE, Context.MODE_PRIVATE);     SharedPreferences.Editor editor = prefs.edit();     editor.clear();     editor.commit(); } 
like image 70
friederbluemle Avatar answered Sep 19 '22 22:09

friederbluemle


You should extend ActivityInstrumentationTestCase2 and use getInstrumentation().getTargetContext() to get the context for the target application being instrumented (under test)

like image 29
Blackbelt Avatar answered Sep 20 '22 22:09

Blackbelt