如何使用内容解析器/提供程序测试课程? [英] How to test class using content resolver/provider?

查看:60
本文介绍了如何使用内容解析器/提供程序测试课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试查询内容解析器的类.

I'm trying to test class that queries content resolver.

我想使用 MockContentResolver 和模拟 query 方法.

问题在于此方法是最终的.我应该怎么办?使用模拟框架?模拟其他课程?预先感谢.

The problem is that this method is final. What should I do? Use mocking framework? Mock other class? Thanks in advance.

public class CustomClass {

    private ContentResolver mContentResolver;

    public CustomClass(ContentResolver contentResolver) {
        mContentResolver = contentResolver;
    }

    public String getConfig(String key) throws NoSuchFieldException {
        String value = null;

            Cursor cursor = getContentResolver().query(...);
            if (cursor.moveToFirst()) {
                //...
            }
        //..
    }
}

推荐答案

这个问题已经很老了,但是人们可能仍然会像我一样面对这个问题,因为没有太多关于测试此问题的文档.

This question is pretty old but people might still face the issue like me, because there is not a lot of documentation on testing this.

对于我来说,对于依赖于内容提供者的测试类(来自android API),我使用了ProviderTestCase2

For me, for testing class which was dependent on content provider (from android API) I used ProviderTestCase2

public class ContactsUtilityTest extends ProviderTestCase2<OneQueryMockContentProvider> {


private ContactsUtility contactsUtility;

public ContactsUtilityTest() {
    super(OneQueryMockContentProvider.class, ContactsContract.AUTHORITY);
}


@Override
protected void setUp() throws Exception {
    super.setUp();
    this.contactsUtility = new ContactsUtility(this.getMockContext());
}

public void testsmt() {
    String phoneNumber = "777777777";

    String[] exampleData = {phoneNumber};
    String[] examleProjection = new String[]{ContactsContract.PhoneLookup.NUMBER};
    MatrixCursor matrixCursor = new MatrixCursor(examleProjection);
    matrixCursor.addRow(exampleData);

    this.getProvider().addQueryResult(matrixCursor);

    boolean result = this.contactsUtility.contactBookContainsContact(phoneNumber);
    // internally class under test use this.context.getContentResolver().query(); URI is ContactsContract.PhoneLookup.CONTENT_FILTER_URI
    assertTrue(result);
}


}


public class OneQueryMockContentProvider extends MockContentProvider {
private Cursor queryResult;

public void addQueryResult(Cursor expectedResult) {
    this.queryResult = expectedResult;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    return this.queryResult;
}
}

它是用詹恩·温加顿的答案写的.几件事要注意:-您的 MockContentProvider 必须是公共的-您必须在测试的类中使用方法 this.getMockContext()中的 Context 而不是 this.getContext()不是模拟数据,而是来自设备的真实数据(在这种情况下-联系人)-测试不得与AndroidJUnit4运行程序一起运行-Test当然必须作为android Instrumented测试运行-测试构造函数(权限)中的第二个参数必须与被测类中查询的URI相同-模拟提供程序的类型必须作为类参数提供

It's written by using Jenn Weingarten's answer. Few things to note: -your MockContentProvider must be public -you must use Context from method this.getMockContext() instead of this.getContext() in your class under test, otherwise you will access not mock data but real data from device (in this case - contacts) -Test must not be run with AndroidJUnit4 runner -Test of course must be run as android instrumented test -Second parameter in constructor of the test (authority) must be same compared to URI queried in class under test -Type of mock provider must be provided as class parameter

基本上ProviderTestCase2可以让您初始化模拟上下文,模拟内容解析器和模拟内容提供程序.

Basically ProviderTestCase2 makes for you initializing mock context, mock content resolver and mock content provider.

我发现使用较旧的测试方法要容易得多,而不是尝试为高度依赖android api的类编写带有嘲笑和junit4的本地单元测试.

I found it much more easier to use older method of testing instead of trying to write local unit test with mockito and junit4 for class which is highly dependent on android api.

这篇关于如何使用内容解析器/提供程序测试课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆