简体   繁体   English

如何在Android中模拟PreferenceManager?

[英]How to mock PreferenceManager in Android?

I've written a class that is using Context , a third party library and SharedPreferences from PreferenceManager . 我编写了一个使用Context ,第三方库和PreferenceManager SharedPreferences

It's possible to mock Context , the third party library can be mocked using some mocking framework, but what to do with PreferenceManager ? 可以模拟Context ,第三方库可以使用一些模拟框架进行模拟,但是如何处理PreferenceManager

I have two methods: 我有两种方法:

public void saveString(ThirdPartyObject obj) {
    SharedPreferences appPreferences = 
        PreferenceManager.getDefaultSharedPreferences(mContext);
    SharedPreferences.Editor editor = appPreferences.edit();
    editor.putString(mContext.getString(
        R.string.preferences_string_name), obj.getString());
    editor.commit();
}

and corresponding, that loads preferences. 和相应的,加载首选项。

It doesn't look like you actually want a mock instance of PreferenceManager (which is mostly used in a PreferenceFragment or PreferenceActivity ). 看起来您实际上并不想要PreferenceManager的模拟实例(主要用于PreferenceFragmentPreferenceActivity )。

You probably want either: 您可能想要:

  1. A mock SharedPreferences , in which case you can just mock Context#getSharedPreferences (which is called by PreferenceManager#getDefaultSharedPreferences anyhow). 一个模拟的SharedPreferences ,在这种情况下你可以模拟Context#getSharedPreferences (无论如何都由PreferenceManager#getDefaultSharedPreferences调用)。 You'll probably also have to make a mock SharedPreferences.Editor if preferences are edited, as above. 如上所述,如果编辑首选项,您可能还必须创建模拟SharedPreferences.Editor You say you already know how to mock the context, so this should be fairly straightforward. 你说你已经知道如何模拟上下文,所以这应该是相当简单的。

  2. To use the actual preferences in the environment. 使用环境中的实际首选项。 This is easiest, and not necessarily a bad idea. 这是最简单的,也不一定是个坏主意。 Do make certain it's cleaned up properly so that your tests don't interfere with each other (or, depending on your test environment, aren't affected by manual use of the app). 确保它已正确清理,以便您的测试不会相互干扰(或者,根据您的测试环境,不受手动使用应用程序的影响)。

If you really do want to mock PreferenceManager instance (like that you get in PreferenceFragment or PreferenceActivity ), you can absolutely do so. 如果你确实想嘲笑PreferenceManager实例(如您在得到PreferenceFragmentPreferenceActivity ),你完全可以这样做。

Since it's non-final, you can generate a mock PreferenceManager and SharedPreferences using Mockito (or another mocking library) as long as you have a way to provide it to your code wherever you would ordinarily get one (in non-test code, this normally comes from the getPreferenceManager() ). 由于它是非final的,你可以使用Mockito(或其他模拟库)生成模拟PreferenceManagerSharedPreferences ,只要你有办法将它提供给你的代码,无论你通常得到一个(在非测试代码中,这通常是来自getPreferenceManager() )。

You can use specialized context for shared preference. 您可以使用专门的上下文来共享首选项。 RenamingDelegatingContext delegates everything to a Context. RenamingDelegatingContext将所有内容委托给Context。 When we access SharedPreference from a Context , we use getSharedPreferences(String name, int mode) . 当我们从Context访问SharedPreference时,我们使用getSharedPreferences(String name, int mode)

Here by extending RenamingDelegatingContext we override getSharedPreferences and pretend the name parameter with test PREFIX , So when test runs it will write to preference file which is different then main application. 这里通过扩展RenamingDelegatingContext我们覆盖getSharedPreferences,假装与测试名称参数PREFIX ,所以测试运行时将会写入偏好文件,该文件是不同的,那么主要的应用。

public class SpecializedMockContext extends RenamingDelegatingContext {
    public static final String PREFIX = "test.";

    public SpecializedMockContext(Context context) {
        super(context, PREFIX);
    }

    @Override
    public SharedPreferences getSharedPreferences(String name, int mode) {
        return super.getSharedPreferences(PREFIX + name, mode);
    }
}

Set this SpecialisedMockContext to your test Application context. 将此SpecialisedMockContext设置为测试应用程序上下文。 setContext(specialisedMockContext) and createApplication() . setContext(specialisedMockContext)createApplication()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM