24

我正在尝试清除测试期间添加的所有 SharedPreferences。我已经阅读了一些帖子和官方文档(SharedPreferences.Editor.clear())。但是当我在单元测试运行后启动我的应用程序时,我仍然找到了测试值。

所以,在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 上发现的所有其他问题都是关于commit()在 之后添加的clear(),我已经在这里完成了。

编辑 1添加setUp()方法

编辑 2提供扩展类

4

4 回答 4

21

如果您使用ActivityTestRule的是Espresso,请尝试以下操作:

@Rule
public ActivityTestRule<MainActivity> activityTestRule =
    new ActivityTestRule<MainActivity>(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();
}
于 2016-01-29T10:35:59.930 回答
13

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

于 2013-07-22T15:48:38.960 回答
4

我用 AndroidJUnit4 运行你的方法,它被称为 @Before 和 @After 时它工作得很好。

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MyBusStopsTest {
    @Rule
    public ActivityTestRule<MyBusStopsActivity> 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()));
    }
 }
于 2016-03-30T09:39:23.480 回答
3

您的测试类应该扩展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();
    }
}
于 2015-10-14T16:57:48.707 回答