[英]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.