![](/img/trans.png)
[英]Is there a way to execute a single Robolectric 2.3 test (using Gradle)?
[英]How to write Robolectric (2.3) test using database
由於Robolectic最后一次發布到版本2.3,它寫的是( https://github.com/robolectric/robolectric/releases ):
Robolectric現在使用SQLite的實際實現而不是陰影和假貨的集合。 現在可以編寫測試來驗證真實的數據庫行為。
我還沒有找到任何“如何”文檔。 我想知道如何使用SQLiteDatabase查詢在Activity上實現測試。 我應該把.db文件放在哪里,以便測試使用它。
您需要將.db文件放在src/test/resources/
文件夾下。
例如, sample.db
然后在你的單元測試setUp()調用:
@Before
public void setUp() throws Exception {
String filePath = getClass().getResource("/sample.db").toURI().getPath();
SQLiteDatabase db = SQLiteDatabase.openDatabase(
(new File(filePath)).getAbsolutePath(),
null,
SQLiteDatabase.OPEN_READWRITE);
// perform any db operations you want here
}
以下是如何測試數據庫,操作的示例。
// just a wrapper for the content values map
Agenda agenda = new Agenda();
agenda.setName("MyAgenda");
agenda.setDate("current date");
long rowId = agendaManager.insert(agenda); // the guy who makes database operations
Cursor query = context.getContentResolver().query(AgendaProvider.AGENDA_CONTENT_URI, null, null, null, null);
assertThat(query.getCount()).isEqualTo(1);
query.moveToNext();
Agenda dbAgenda = new Agenda(query);
assertThat(dbAgenda.getRowId()).isPositive();
assertThat(dbAgenda.getRowId()).isEqualTo(rowId);
assertThat(dbAgenda.getName()).isEqualTo(agenda.getName());
assertThat(dbAgenda.getDate()).isEqualTo(agenda.getDate());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.