本文介紹了如何使用 Mockito 模擬 SharedPreferences的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我剛剛閱讀了有關 Android 中的單元檢測測試,我想知道如何在沒有任何 SharedPreferencesHelper 類的情況下模擬 SharedPreferences,例如 這里
I have just read about Unit Instrumented Testing in Android and I wonder how I can mock a SharedPreferences without any SharedPreferencesHelper class on it like here
我的代碼是:
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;
}}
是否可以在不創建其他類的情況下模擬 SharedPreferences?
Is is possible to mock SharedPreferences without creating other class on it?
推薦答案
所以,因為 SharedPreferences
來自您的 context
,所以很簡單:
So, because SharedPreferences
comes from your context
, it's easy:
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
例如,對于 getValidToken(Context context)
,測試可以是:
for example, for getValidToken(Context context)
, the test could be:
@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();
}
這篇關于如何使用 Mockito 模擬 SharedPreferences的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!