ada*_*a88 21 android mockito sharedpreferences
我刚刚阅读了Android中的单元仪表测试,我想知道如何在没有任何SharedPreferencesHelper类的情况下模拟SharedPreferences,就像这里一样
我的代码是:
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;
}}
Run Code Online (Sandbox Code Playgroud)
是否可以在不创建其他类的情况下模拟SharedPreferences?
小智 56
所以,因为SharedPreferences来自你的context,很容易:
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
Run Code Online (Sandbox Code Playgroud)
例如,对于getValidToken(Context context),测试可能是:
@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();
}
Run Code Online (Sandbox Code Playgroud)
Goo*_*ian 11
以下示例显示了如何创建使用模拟 Context 对象(例如共享首选项)的单元测试。
@RunWith(MockitoJUnitRunner.class)
public class MProfileTest {
@Mock
Context mockContext;
@Mock
SharedPreferences mockPrefs;
@Mock
SharedPreferences.Editor mockEditor;
@Before
public void before() throws Exception {
Mockito.when(mockContext.getSharedPreferences(anyString(), anyInt())).thenReturn(mockPrefs);
Mockito.when(mockContext.getSharedPreferences(anyString(), anyInt()).edit()).thenReturn(mockEditor);
Mockito.when(mockPrefs.getString("YOUR_KEY", null)).thenReturn("YOUR_VALUE");
}
@Test
public void anyTest() {
// Any shared preference you can call
// Assert.assertTrue();
String val = _mockPrefs.getString("YOUR_KEY", null); // It returns YOUR_VALUE
}
}
Run Code Online (Sandbox Code Playgroud)
如果您在导入模拟框架时遇到任何问题,请确保您已在app/build.gradle文件中添加了依赖项。
https://developer.android.com/training/testing/unit-testing/local-unit-tests#setup
如果您想通过将所有数据存储在内存中来使用真正的共享偏好作为您的设备,请按照以下代码操作。
从这个 Gist https://gist.github.com/aslamanver/f74a2b3d450fda251d47a0d38b44edb7获取 MockSharedPreference.java 文件
@Mock
Context mockContext;
MockSharedPreference mockPrefs;
MockSharedPreference.Editor mockPrefsEditor;
@Before
public void before() {
mockPrefs = new MockSharedPreference();
mockPrefsEditor = mockPrefs.edit();
Mockito.when(mockContext.getSharedPreferences(anyString(), anyInt())).thenReturn(mockPrefs);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20197 次 |
| 最近记录: |