gpt4 book ai didi

android - 从单元测试调用 getSharedPreferences() 时出现 UnsupportedOperationException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:08:25 26 4
gpt4 key购买 nike

我已经实现了从安装在网络上的服务中检索数据的内容提供程序。内容提供者从共享首选项(用户凭据、应用程序名称等)中获取所需参数

SharedPreferences settings = getContext().getSharedPreferences(NET_PREFS, 0);

我对这个内容提供者使用单元测试:


public class ResearchProviderTest extends ProviderTestCase2<MyProvider>{

public ResearchProviderTest() {
super(MyProvider.class, MyProvider.CONTENT_AUTHORITY);
}

public void testInsert() {

ContentValues values = new ContentValues();
values.put(QueryFields.FIELD_QUERY_TEXT, "query" );

Uri newUri = getMockContentResolver().insert(MyProvider.CONTENT_URI, values);

Cursor readMessagesCursor = getMockContentResolver().query(
newUri ,
new String[] { FIELD_SR_TITLE },
null, null, null );
assertTrue("The cursor should contain some entries", readMessagesCursor.moveToFirst());
}
}

在执行单元测试期间。抛出异常:


java.lang.UnsupportedOperationException
at android.test.mock.MockContext.getSharedPreferences(MockContext.java:127)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
at com.company.mobile.android.net.RemoteExecutor.retrieveCredentials(RemoteExecutor.java:171)
at com.company.mobile.android.net.service.Adapter.executeSearch(Adapter.java:33)
at com.company.mobile.android.provider.MyProvider.insert(MyProvider.java:167)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:174)
...

有人知道如何将共享首选项传递给 MockContext 吗?如果我在测试 Content Provider 时需要 Context,我应该使用其他方法吗?

最佳答案

您必须使用 AndroidTestCase.setContext() 注入(inject)扩展 MockContextContext并至少覆盖 getPackageName()getSharedPreferences()

不幸的是,无法将 MockContext 注入(inject)到 ProviderTestCase2 中,因为 setContext() 不可用。然后,解决方案是重写 setUp() 而不是调用 super 方法。

package com.example.aatg.ps.test;

import android.test.ProviderTestCase2;

import com.example.aatg.ps.ProviderSample;

public class ProviderSampleTests extends ProviderTestCase2<ProviderSample> {

private ResolverRenamingMockContext mProviderContext;
private Class<ProviderSample> mProviderClass;
private ProviderSample mProvider;
private String mProviderAuthority;

public ProviderSampleTests() {
super(ProviderSample.class, ProviderSample.AUTHORITY);
setName("ProviderSampleTests");
mProviderClass = ProviderSample.class;
mProviderAuthority = ProviderSample.AUTHORITY;
}

@Override
protected void setUp() throws Exception {
// We should not invoke super.setUp() here to avoid the UnsupportedOperationException.
//super.setUp();

// This is needed to support getSharedPreferences() in ProviderSample.onCreate()
mProviderContext = new ResolverRenamingMockContext(getContext());
mProvider = mProviderClass.newInstance();
assertNotNull(mProvider);
mProvider.attachInfo(mProviderContext, null);
mProviderContext.addProvider(mProviderAuthority, mProvider);
}


@Override
public ProviderSample getProvider() {
return mProvider;
}

protected void tearDown() throws Exception {
super.tearDown();
}

public void testProviderSampleCreation() {
ProviderSample provider = getProvider();
assertNotNull(provider);
}
}

这是ResolverRenamingMockContext:

package com.example.aatg.ps.test;

import java.io.File;

import android.content.ContentProvider;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.test.IsolatedContext;
import android.test.mock.MockContentResolver;
import android.test.mock.MockContext;

/**
* @author diego
*
*/
public class ResolverRenamingMockContext extends IsolatedContext {

/**
* The renaming prefix.
*/
private static final String PREFIX = "test.";


/**
* The resolver.
*/
private static final MockContentResolver RESOLVER = new MockContentResolver();

/**
* Constructor.
* @param context
*/
public ResolverRenamingMockContext(Context context) {
super(RESOLVER, new DelegatedMockContext(context));
}

public MockContentResolver getResolver() {
return RESOLVER;
}

public void addProvider(String name, ContentProvider provider) {
RESOLVER.addProvider(name, provider);
}

/**
* The DelegatedMockContext.
*
*/
private static class DelegatedMockContext extends MockContext {

private Context mDelegatedContext;

public DelegatedMockContext(Context context) {
mDelegatedContext = context;
}

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

}

关于android - 从单元测试调用 getSharedPreferences() 时出现 UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5267671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com