Próbuję wyczyścić wszystkie SharedPreferences dodane podczas moich testów. Przeczytałem już kilka postów i oficjalną dokumentację (SharedPreferences.Editor.clear()). Ale kiedy uruchomiłem aplikację po uruchomieniu testów jednostkowych, nadal znajdowałem wartości testowe.Test urządzeń Android: jak wyczyścić SharedPreferences
Więc w AndroidTestCase.tearDown(), robię to:
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();
}
}
co drugi znalazłem na pytania, więc było o dodawaniu commit()
po clear()
, który ja już wykonanej tutaj.
EDIT 1 Dodawanie setUp()
metoda
EDIT 2 Zapewnienie rozszerzoną klasę
Czy używasz tego samego kontekstu do wklejania i czyszczenia SharedPreferences? – Blackbelt
Kontekst jest zgodny z klasą AndroidTestCase. Ponadto, kiedy uruchamiam moją aplikację, wciąż nazywam 'getContext()'. Jeśli chcesz, mogę podać metodę "setUp()" moich testów jednostkowych. – mithrop
tak, prawdopodobnie jest lepiej. Czy zdajesz sobie sprawę, że po uruchomieniu testów inna apk jest aktualizacją testu urządzenia? – Blackbelt