2016-01-30 11 views
11

Właśnie przeczytałem o jednostce oprzyrządowanego próbujący w Androidzie i zastanawiam się, jak mogę kpić do SharedPreferences bez jakiejkolwiek klasy SharedPreferencesHelper na nim jak hereJak mock a SharedPreferences użyciu Mockito

Mój kod to:

public class Auth { 
private static SharedPreferences loggedUserData = null; 
public static String getValidToken(Context context) 
{ 
    initLoggedUserPreferences(context); 
    String token = loggedUserData.getString(Constants.USER_TOKEN,null); 
    return token; 
} 
public static String getLoggedUser(Context context) 
{ 
    initLoggedUserPreferences(context); 
    String user = loggedUserData.getString(Constants.LOGGED_USERNAME,null); 
    return user; 
} 
public static void setUserCredentials(Context context, String username, String token) 
{ 
    initLoggedUserPreferences(context); 
    loggedUserData.edit().putString(Constants.LOGGED_USERNAME, username).commit(); 
    loggedUserData.edit().putString(Constants.USER_TOKEN,token).commit(); 
} 

public static HashMap<String, String> setHeaders(String username, String password) 
{ 
    HashMap<String, String> headers = new HashMap<String, String>(); 
    String auth = username + ":" + password; 
    String encoding = Base64.encodeToString(auth.getBytes(), Base64.DEFAULT); 
    headers.put("Authorization", "Basic " + encoding); 
    return headers; 
} 

public static void deleteToken(Context context) 
{ 
    initLoggedUserPreferences(context); 
    loggedUserData.edit().remove(Constants.LOGGED_USERNAME).commit(); 
    loggedUserData.edit().remove(Constants.USER_TOKEN).commit(); 
} 

public static HashMap<String, String> setHeadersWithToken(String token) { 
    HashMap<String, String> headers = new HashMap<String, String>(); 
    headers.put("Authorization","Token "+token); 
    return headers; 
} 
private static SharedPreferences initLoggedUserPreferences(Context context) 
{ 
    if(loggedUserData == null) 
     loggedUserData = context.getSharedPreferences(Constants.LOGGED_USER_PREFERENCES,0); 
    return loggedUserData; 
}} 

Czy można kpić SharedPreferences bez tworzenia na nim innych klas?

Odpowiedz

34

Tak, ponieważ SharedPreferences pochodzi z context, to proste:

final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); 
final Context context = Mockito.mock(Context.class); 
Mockito.when(context.getSharedPreferences(anyString(), anyInt()).thenReturn(sharedPrefs); 

// no use context 

na przykład dla getValidToken(Context context), test może być:

@Before 
public void before() throws Exception { 
    this.sharedPrefs = Mockito.mock(SharedPreferences.class); 
    this.context = Mockito.mock(Context.class); 
    Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); 
} 

@Test 
public void testGetValidToken() throws Exception { 
    Mockito.when(sharedPrefs.getString(anyString(), anyString())).thenReturn("foobar"); 
    assertEquals("foobar", Auth.getValidToken(context)); 
    // maybe add some verify(); 
} 
+1

Ok, dziękuję. Co powiesz na 'public static void setUserCredentials (Kontekst kontekstowy, Nazwa użytkownika String, String token)' test? Chciałbym ustawić poświadczenia użytkownika przed 'testGetValidToken()' Przypuszczam, że potrzebuję makiety dla 'SharedPreferences.Editor', ale jeśli zapiszę niektóre klucza przez to fałszywe będzie on dostępny przez makra' SharedPreferences'? – adamura88

+1

Po wywołaniu 'edit' zwróciłem' Editor'a. "będzie dostępny przez makiety SharedPreferences?" nie, udawanie jest głupim obiektem –

+0

Twój kod zawiera błędy składniowe. –