Test and debug your database

When you create databases using the Room persistence library, you should verify the stability of your app's database and your users' data. This page describes how to test your database and perform debugging steps to help your tests pass.

Test your database

There are two ways to test your database:

  • On a device that runs Android
  • On your host development machine

For information about testing that's specific to database migrations, see Testing Migrations.

Test on a device that runs Android

To test your database implementation, we recommend that you write a JUnit test that runs on a device that runs Android. Because these tests don't require an activity, they run faster than UI tests.

When setting up your tests, you should create an in-memory version of your database to make your tests more hermetic, as shown in the following example:

import kotlinx.coroutines.test.runTest

@RunWith(AndroidJUnit4::class)
class SimpleEntityReadWriteTest {
    private lateinit var userDao: UserDao
    private lateinit var db: TestDatabase

    @Before
    fun createDb() {
        val context = ApplicationProvider.getApplicationContext<Context>()
        db = Room.inMemoryDatabaseBuilder<TestDatabase>(context)
                .setDriver(BundledSQLiteDriver())
                .build()
        userDao = db.userDao()
    }

    @After
    fun closeDb() {
        db.close()
    }

    @Test
    fun writeUserAndReadInList() = runTest {
        val user = User(3, "george")
        userDao.insert(user)
        val byName = userDao.findUsersByName("george")
        assertEquals(byName.single(), user)
    }
}

Test on your host machine (JVM)

With Room KMP, you can run database tests on your host development machine using local JVM JUnit tests. This setup lets your tests run faster by eliminating the overhead of an Android emulator or device.

To help maintain consistency, use the BundledSQLiteDriver. This driver includes SQLite compiled from source, which uses the exact same version of SQLite on your host machine, Android devices, and iOS devices. Using this driver eliminates version mismatch issues.

To set up JVM tests, configure the BundledSQLiteDriver in your test database builder:

val db = Room.inMemoryDatabaseBuilder<TestDatabase>()
    .setDriver(BundledSQLiteDriver())
    .build()

Test your migrations

Room supports incremental database migrations to retain existing app data in situations where an app update changes the database schema. However, an incorrectly defined migration could cause your app to crash. Make sure that you test your Room database migrations.

Debug your database

There are several tools and processes that you can use to debug your database.

Use the Database Inspector

In Android Studio 4.1 and higher, the Database Inspector lets you inspect, query, and modify your app's databases while your app runs. The Database Inspector is compatible with the version of SQLite that is bundled with Android and includes special features for use with Room:

  • Use gutter actions to run queries from your DAO classes.
  • When your running app makes changes to the data, you can immediately see live updates in the Database Inspector.

For more information about the Database Inspector, see Debug your database with the Database Inspector.

Dump data from the command line

The Android SDK includes a sqlite3 database tool for examining your app's databases. It includes commands such as .dump to print the contents of a table, and .schema to print the SQL CREATE statement for an existing table.

You can also execute SQLite commands from the command line, as shown in the following snippet:

adb -s emulator-5554 shell sqlite3
/data/data/your-app-package/databases/rssitems.db

For more information, see the sqlite3 command line documentation, available on the SQLite website.

Additional resources

To learn more about testing and debugging your Room database, see the following additional resources:

Blog posts

Videos