com.android.providers.test,如何在Android上測試內容提供商

13

As far as I found, setting up the mock content resolver is not explicitly necessary - I might oversee cases where it is(maybe correct resolving of the provider via URI, hings that need corect getType() working), but for me, it was enough to do something like this:

據我所知,設置模擬內容解析器並不是必需的 - 我可以監督它的情況(可能通過URI正確解析提供者,需要corect getType()工作的hings,但對我來說,它是足以做這樣的事情:

package org.droidcon.apps.template.provider.test;

import org.droidcon.apps.template.provider.ProfileContract;

import org.droidcon.apps.template.provider.ProfileProvider;

import android.content.ContentProvider;

import android.content.ContentValues;

import android.database.Cursor;

import android.net.Uri;

import android.test.ProviderTestCase2;

public class ProfileProviderTest extends ProviderTestCase2 {

public ProfileProviderTest() {

super(ProfileProvider.class, ProfileProvider.class.getName());

}

protected void setUp() throws Exception {

super.setUp();

}

/**

* Very basic query test.

*

* Prerequisites:

*

*

A provider set up by the test framework

*

*

* Expectations:

*

*

a simple query without any parameters, before any inserts returns a

* non-null cursor

*

a wrong uri results in {@link IllegalArgumentException}

*

*/

public void testQuery(){

ContentProvider provider = getProvider();

Uri uri = ProfileContract.CONTENT_URI;

Cursor cursor = provider.query(uri, null, null, null, null);

assertNotNull(cursor);

cursor = null;

try {

cursor = provider.query(Uri.parse("definitelywrong"), null, null, null, null);

// we're wrong if we get until here!

fail();

} catch (IllegalArgumentException e) {

assertTrue(true);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值