Ich habe begonnen, für meine Content-Provider einen einfachen Test zu schreiben. Das Problem ist, wenn ich den Test verwende, verwendet es Produktionsdaten. Wie kann ich sicherstellen, dass separate Testdaten aus meinen Live-App-Daten verwendet werden?Content Prüfung - Trennung von Produktion und Testdaten
@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;
}
}
ich nicht bekommen, was Sie archivieren wollen? .. Könnten Sie vielleicht sagen, was die Dinge tun sollten und was sie stattdessen tun? – creativecreatorormaybenot
Ist 'MyContentProvider.uri' die echte Produktion' Uri'? – CommonsWare
@CommonsWare, ja ist es. Ich habe es als eine Mitgliedsvariable von MyContentProvider, sollte ich einen anderen URI verwenden? – Stephen