【发布时间】:2017-01-14 20:11:21
【问题描述】:
我已经开始为我的内容提供者编写一个简单的测试。问题是当我运行测试时它使用的是生产数据。如何确保使用单独的测试数据与我的实时应用数据?
@RunWith(AndroidJUnit4.class)
public class MyContentProviderTest extends ProviderTestCase2<MyContentProvider>{
public MyContentProviderTest() {
super(MyContentProvider.class, MyContentProvider.AUTHORITY);
}
@Override
protected void setUp() throws Exception {
setContext(InstrumentationRegistry.getContext());
//have also tried with setContext(InstrumentationRegistry.getTargetContext());
super.setUp();
}
@Test
public void insertTest(){
ContentResolver contentResolver = getContext().getContentResolver();
assertNotNull(contentResolver);
contentResolver.insert(MyContentProvider.uri,createContentValues());
Cursor cursor = contentResolver.query(MyContentProvider.uri, Database.ALL_COLUMNS,
null, null, null);
assertNotNull(cursor);
// the test fails here because along with the row inserted above, there are also many more rows of data from using my app normally (not while under test).
assertEquals( 1, cursor.getCount());
//todo: verify cursor contents
cursor.close();
}
ContentValues createContentValues(){
ContentValues cv = new ContentValues();
cv.put(Database.COLUMN_DATETIME, LocalDateTime.now().format(Util.DATE_FORMAT));
/* ... etc */
return cv;
}
}
【问题讨论】:
-
我没有得到你想要归档的内容?.. 你能不能说一下应该做什么以及他们正在做什么?
-
MyContentProvider.uri是真正的生产Uri吗? -
@CommonsWare,是的。我将它作为 MyContentProvider 的成员变量,我应该使用不同的 URI 吗?
-
@creativecreatorormaybenot 我有一个使用我的应用程序的实时数据的测试。我正在尝试了解如何在运行测试时使用单独的测试数据
标签: android unit-testing testing android-contentprovider