2013-07-22 17 views
15

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ę

+0

Czy używasz tego samego kontekstu do wklejania i czyszczenia SharedPreferences? – Blackbelt

+0

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

+0

tak, prawdopodobnie jest lepiej. Czy zdajesz sobie sprawę, że po uruchomieniu testów inna apk jest aktualizacją testu urządzenia? – Blackbelt

Odpowiedz

2

Testowana klasa powinna rozszerzyć się na InstrumentationTestCase.

Powinieneś użyć getInstrumentation().getTargetContext().

Jeśli chcesz bezpośrednio manipulować aktywności, Twoja klasa testowa powinna rozszerzyć się na ActivityInstrumentationTestCase2.

Więc twój przypadek testowy powinien wyglądać następująco:

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

Jeśli używasz ActivityTestRule z Espresso, spróbuj tego:

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

z nieco zmodyfikowaną wersją stevo.mit na 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(); 
} 
+0

Jedno dodanie: Ponieważ twoja metoda 'clearSharedPrefs() jest niezależna od testuj klasę, powinieneś przenieść ją do klasy pomocniczej i upublicznić ją, abyś mógł ponownie jej użyć. – tir38

+0

powinno to być zaakceptowaną odpowiedzią, ponieważ ActivityInstrumentationTestCase2 jest już przestarzałe – Khriz

0

Uruchomię twoją metodę za pomocą AndroidJUnit4 i działa idealnie, gdy nazywa się je @Before i @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())); 
    } 
} 
Powiązane problemy