如何使用Mockito模拟SharedPreferences

编程入门 行业动态 更新时间:2024-10-26 18:22:03
本文介绍了如何使用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)mit(); loggedUserData.edit().putString(Constants.USER_TOKEN,token)mit(); } 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)mit(); loggedUserData.edit().remove(Constants.USER_TOKEN)mit(); } 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

本文发布于:2023-10-31 07:12:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Mockito   SharedPreferences

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!