我使用 espresso 来清除我的应用程序中的数据库
我像这样设置活动
@Rule
@JvmField
val activity = ActivityTestRule<PhotoPrinterActivity>(PhotoPrinterActivity::class.java,false,false)
这是我之前的功能
@Before
open fun setup() {
clearDatabase()
activity.launchActivity(null)
// Waiting for start app success fully
}
这是我清晰的数据库代码
fun clearDatabase() {
val databaseList = InstrumentationRegistry.getInstrumentation().targetContext.databaseList()
for (database in databaseList) {
// when transaction rollback files exists they are always locked so we can't delete them
if (database.contains(".db-journal")) {
InstrumentationRegistry.getTargetContext().deleteDatabase(database)
continue
}
// when using transaction write ahead logging then this db files are listed but often they don't exist
if (database.contains(".db-wal") || database.contains(".db-shm")) {
InstrumentationRegistry.getTargetContext().deleteDatabase(database)
continue
}
Log.v("EspressoMacchiato", "deleting " + database)
var databasePath = InstrumentationRegistry.getInstrumentation().targetContext.getDatabasePath(database)
if (databasePath.exists()) {
InstrumentationRegistry.getInstrumentation().targetContext.deleteDatabase(database)
}
}
}
问题是当清除数据库成功并执行将一些数据添加到数据库时,
android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:786)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
任何人请帮助我!非常感谢!
Use @课前 http://junit.sourceforge.net/javadoc/org/junit/BeforeClass.html and 仪器注册中心 https://developer.android.com/reference/android/support/test/InstrumentationRegistry.html在您的 espresso 测试中删除数据库:
@BeforeClass
public static void beforeClass() {
InstrumentationRegistry.getTargetContext().deleteDatabase("database_name");
}
为了防止同时执行多个浓缩咖啡测试时出现错误,请使用Android 测试协调器 https://developer.android.com/training/testing/junit-runner.html#using-android-test-orchestrator。它将分别执行所有这些。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)