本篇文章主要讲解两个练习,如何测试Content Provider和Application
练习: 测试Content Provider
下面是一个Content Provider 的测试案例 (原文中并无此案例)
假设存在一个 ContentProvider, 支持插入数据和查询数据 (仅为测试, 无实际意义,为减少篇幅已删除部分非关键代码)
package com.lulu.androidtestdemo.instrumentation;
// more
public class MyContentProvider extends ContentProvider {
public static final String AUTHORITIES = "com.lulu.androidtestdemo.MyContentProvider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITIES + "/content");
private SQLiteOpenHelper mDbHelper;
public static final int CODE_TEST = 1;
private static final UriMatcher mUriMatcher;
static{
mUriMatcher = new UriMatcher(0);
// 添加 Uri的匹配规则,实现 uri 对应判断是哪个表的操作
mUriMatcher.addURI("*", "/content", CODE_TEST);
}
public MyContentProvider() {
}
@Override
public boolean onCreate() {
mDbHelper = new DbHelper(getContext());
return true;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
Uri ret = null;
int code = mUriMatcher.match(uri);
SQLiteDatabase db = mDbHelper.getWritableDatabase();
switch (code) {
case CODE_TEST:
long id = db.insert(TEST_TABLE_NAME, null, values);
// 数据库添加完成之后返回的 ID,必须和 Uri参数拼接在一起,再返回
ret = ContentUris.withAppendedId(uri, id);
break;
}
db.close();
return ret;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Cursor ret = null;
SQLiteDatabase db = mDbHelper.getReadableDatabase();
int code = mUriMatcher.match(uri);
switch (code) {
case CODE_TEST:
ret = db.query(TEST_TABLE_NAME, projection, selection,
selectionArgs, null, null, sortOrder);
break;
}
return ret;
}
// more
public SQLiteOpenHelper getDbHelper() {
return mDbHelper;
}
public static final String TEST_TABLE_NAME = "testTable";
public static final String TEST_TABLE_COL_CONTENT = "content";
public class DbHelper extends SQLiteOpenHelper {
private static final String CREATE_TABLE_COST =
"create table if not exists " + TEST_TABLE_NAME +
"( _id integer primary key autoincrement," +
TEST_TABLE_COL_CONTENT + " text" +
")";
public DbHelper(Context context) {
super(context, "myapp", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_COST);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
通过继承 ProviderTestCase2 类, 测试 insert 方法是否生效, 本测试用例执行通过.
/**
* Created by zhanglulu on 2018/3/2.
*/
@RunWith(AndroidJUnit4.class)
public class MyContentProviderTest extends ProviderTestCase2<MyContentProvider>{
public MockContentResolver mMockResolver;
public MyContentProviderTest() {
super(MyContentProvider.class, MyContentProvider.AUTHORITIES);
}
@Before
@Override
public void setUp() throws Exception {
setContext(InstrumentationRegistry.getTargetContext());
super.setUp();
mMockResolver = getMockContentResolver();
}
@Test
public void insert() throws Exception {
ContentValues values = new ContentValues();
values.put(MyContentProvider.TEST_TABLE_COL_CONTENT, "保存测试信息");
Uri insert = mMockResolver.insert(MyContentProvider.CONTENT_URI, values);
Cursor cursor = mMockResolver.query(MyContentProvider.CONTENT_URI,
null, null,
null, null);
assertTrue(cursor.moveToNext());
assertEquals(1, cursor.getCount());
String testStr =
cursor.getString(cursor.getColumnIndex(MyContentProvider.TEST_TABLE_COL_CONTENT));
assertEquals(testStr, "保存测试信息");
}
}
注意: 写上面的这个测试用例非常的坑 !!!
- @Before和@Test 两个注解务必要加上, 网上找到很多资料都是比较旧的没有加上这两个注解导致测试无法运行.
- 最坑的是上述的setContext()方法务必添加, 而且添加的位置务必在super.setUp()方法之前. 官方文档竟然放在了这个super方法的下面!导致无法运行测试
截图为证:
练习: 测试Application
创建工程
创建一个待测试Application , 并且不要忘记在AndroidMainfest.xml中声明.
public class MyApplication extends Application {
public static final List<String> list = new ArrayList<String>();
}
对Application对象创建本地单元测试
在 src/src/test 目录下创建单元测试. 验证 MyApplication.list 字段不为null.
对Application对象创建 Instrumented 测试
创建下面基于JUnit 3测试框架的Instrumented测试
package com.lulu.androidtestdemo.instrumentation;
import android.content.pm.PackageInfo;
import android.test.ApplicationTestCase;
import android.test.MoreAsserts;
import com.lulu.androidtestdemo.MyApplication;
import org.junit.Test;
/**
* Created by zhanglulu on 2018/2/26.
*/
public class ApplicationTest extends ApplicationTestCase<MyApplication> {
public MyApplication application;
public ApplicationTest() {
super(MyApplication.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
createApplication();
application = getApplication();
}
@Test
public void testCorrectVersion() throws Exception {
PackageInfo info = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);
assertNotNull(info);
MoreAsserts.assertMatchesRegex("\\d\\.\\d", info.versionName);
}
}