'Clear database before testcase espresso

im using espresso to clear database in my app Im setting activity like this

@Rule
@JvmField
val activity = ActivityTestRule<PhotoPrinterActivity>(PhotoPrinterActivity::class.java,false,false)

And this is my before function

@Before
open fun setup() {
    clearDatabase()
    activity.launchActivity(null)
    // Waiting for start app success fully

}

And this is my clear database code

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)
        }
    }

}

Issue is when clear database success and perform add some data into 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)

Anyone please help me !Thanks so much!



Solution 1:[1]

In case you can't make the accepted answer work due to deprecation I believe the Instrumentation api was updated to: InstrumentationRegistry .getInstrumentation() .getTargetContext()

therefore:

InstrumentationRegistry.getInstrumentation().getTargetContext().deleteDatabase("database_name")

Alternatively you can get the actual application context (the "target") you can do :

ApplicationProvider.getApplicationContext<YOUR-APPLICATION>().deleteDatabase("database_name")

Solution 2:[2]

Clearing the database once per test class

Add the following code to your Android Test class:

companion object {
    @BeforeClass
    fun clearDatabase() {
        InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand("pm clear PACKAGE_NAME").close()
    }
}

Clearing the database before every test

An alternative way to have the database cleared before each test run is to set the clearPackageData flag while using Android Test Orchestrator. This will "remove all shared state from your device's CPU and memory after each test:"

Add the following statements to your project's build.gradle file:

android {
  defaultConfig {
   ...
   testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

   // The following argument makes the Android Test Orchestrator run its
   // "pm clear" command after each test invocation. This command ensures
   // that the app's state is completely cleared between tests.
   testInstrumentationRunnerArguments clearPackageData: 'true'
 }

  testOptions {
    execution 'ANDROIDX_TEST_ORCHESTRATOR'
  }
}

dependencies {
  androidTestImplementation 'androidx.test:runner:1.1.0'
  androidTestUtil 'androidx.test:orchestrator:1.1.0'
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HenriqueMS
Solution 2