私は自分のコンテンツプロバイダの簡単なテストを書いています。問題は、実動データを使用しているテストを実行するときです。ライブアプリデータから別のテストデータを使用するにはどうすればよいですか?ContentProviderテスト - 生産とテストデータの分離
@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;
}
}
アーカイブしようとしているものがありませんか? – creativecreatorormaybenot
'MyContentProvider.uri'は本当の生産物' Uri'ですか? – CommonsWare
@CommonsWare、そうです。 MyContentProviderのメンバー変数として、別のURIを使用する必要がありますか? – Stephen