Androidunit testing:如何清除SharedPreferences

我试图清除在测试期间添加的所有SharedPreferences。 我已经阅读了一些post和官方文档( SharedPreferences.Editor.clear() )。 但是当我运行unit testing后启动应用程序时,我仍然find了测试值。

所以,在AndroidTestCase.tearDown()中 ,我这样做:

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(); } } 

我在SO上发现的其他问题都是关于在clear()之后添加commit() clear() ,我已经在这里做了。

编辑1添加setUp()方法

编辑2提供扩展课程

您应该扩展ActivityInstrumentationTestCase2并使用getInstrumentation().getTargetContext()来获取正在检测的目标应用程序的上下文(在测试中)

如果您使用Espresso的 ActivityTestRule ,请尝试以下方法:

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

使用稍微修改过的版本的stevo.mit的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(); } 

你测试类应该扩展InstrumentationTestCase

你应该使用getInstrumentation().getTargetContext()

如果需要直接操作活动 ,那么测试类应该扩展ActivityInstrumentationTestCase2

所以你的测试用例应该是这样的:

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

我使用AndroidJUnit4运行你的方法,当它被称为@Before和@After时,它的工作非常完美。

 @RunWith(AndroidJUnit4.class) @LargeTest public class MyBusStopsTest { @Rule public ActivityTestRule mActivityRule = new ActivityTestRule<>( MyBusStopsActivity.class); @Before @After public void cleanSheredPrefs(){ SharedPreferences sharedPreferences = getInstrumentation().getTargetContext().getSharedPreferences(MyBusStopsActivity.FAV_LIST, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.clear(); editor.commit(); } @Test public void showChooseBusStopActivityOnFABClick() { onView(withId(R.id.fab)).perform(click()); onView(withChild(withId(R.id.choose_bus_button))).check(matches(isDisplayed())); } }