How to Test Room Database in Jetpack Compose?

Emine İNAN
Huawei Developers
Published in
4 min readAug 1, 2024
Photo by Alexander Mils on Unsplash

Introduction 🌟

Writing tests for a Room database in Jetpack Compose is crucial to ensure the app’s reliability and performance. In my previous article, we explored how to integrate Room and Hilt into a Jetpack Compose project to manage local data efficiently. Building on that foundation, this article will guide you through writing effective tests for the Room database, ensuring your data operations are accurate and robust.

Benefits of Testing the Room Database

Testing the Room database ensures data integrity by verifying that operations like insertions, updates, deletions, and queries are performed correctly. It validates the database schema and entity relationships, ensuring consistency and accuracy. Additionally, tests help identify performance issues and provide confidence that changes to the database do not break existing functionality.

Testing Room Database in the QuoteApp

In Android, tests for the Room database are typically written under the androidTest directory. The androidTest directory is used for instrumentation tests that run on an Android device or emulator. These tests are suitable for realistically testing database operations, DAOs, and other Android components.

The QuoteApp built with Jetpack Compose is a sample app that contains the Room Database and Hilt. In this section, we will explore how to effectively test the Room database in this app.

QuoteApp

To start testing the QuoteApp, you can access the project’s starter code here.

Let’s start!

Step 1

🚩 Add the dependencies required for testing in your build.gradle file.

build.gradle.kts (App)

Step 2

🚩 Create the QuoteDaoTest class under the androidTest folder as the main class where you will write your test methods for the QuoteDao.

Annotate the class with @RunWith(AndroidJUnit4::class) to specify using the AndroidJUnit4 test runner for Android, and declare lateinit variables quoteDao and quoteDatabase to be initialized and used for accessing the DAO and database within the tests.

QuoteDaoTest.kt

Step 3

🚩 Add the createDb method to set up the in-memory database and DAO before each test.

Annotate the method with @Before to run it before each test, define the createDb method to initialize the database and DAO, get the application context using ApplicationProvider.getApplicationContext<Context>(), create an in-memory instance of QuoteDatabase using Room.inMemoryDatabaseBuilder(context, QuoteDatabase::class.java).build() to avoid data persistence, and initialize the quoteDao variable by calling quoteDatabase.quoteDao().

QuoteDaoTest.kt

Step 4

🚩 Add the closeDb method to clean up the database after each test.

Annotate the method with @After to run it after each test, define the closeDb method to close the database, and call quoteDatabase.close() within it to release resources.

QuoteDaoTest.kt

Step 5

🚩 Add the quoteDao_insert_and_retrieve_quotes test method to verify the insertion and retrieval of quotes.

Annotate the method with @Test to indicate it is a test method, define quoteDao_insert_and_retrieve_quotes using runTest for coroutine execution, create quote1 and quote2 instances with specific IDs and text, insert them into the database with quoteDao.addQuote(quote1) and quoteDao.addQuote(quote2), retrieve the quotes using quoteDao.getAll().first(), and use assertTrue(quotes.contains(quote1)), assertTrue(quotes.contains(quote2)), and assertEquals(2, quotes.size) to verify their presence and count.

QuoteDaoTest.kt

Step 6

🚩 Add the quoteDao_delete_quote test method to verify the deletion of a quote.

Annotate the method with @Test to indicate it is a test method, define quoteDao_delete_quote using runTest for coroutine execution, create a Quote instance named quote, insert it with quoteDao.addQuote(quote), delete it with quoteDao.deleteQuote(quote), retrieve the quotes using quoteDao.getAll().first(), and use assertFalse(quotes.contains(quote)) to ensure the deleted quote is no longer on the list.

QuoteDaoTest.kt

Step 7

🚩 Add the quoteDao_insert_delete_and_retrieve_quotes test method to verify the insertion, deletion, and retrieval of quotes.

Annotate the method with @Test to indicate it is a test method, define quoteDao_insert_delete_and_retrieve_quotes using runTest for coroutine execution, create two Quote instances (quote1 and quote2), insert them with quoteDao.addQuote(quote1) and quoteDao.addQuote(quote2), delete quote2 with quoteDao.deleteQuote(quote2), retrieve quotes using quoteDao.getAll().first(), and use assertTrue(quotes.contains(quote1)), assertFalse(quotes.contains(quote2)), and assertEquals(1, quotes.size) to verify the operations.

QuoteDaoTest.kt

Step 8

🚩 To run the test class, right-click on the QuoteDaoTest class file and select Run 'QuoteDaoTest'; view the test results in the Run window at the bottom of Android Studio to ensure all tests pass, and use the green play button next to the test class or individual methods as an alternative method.

Test Results
Animated GIF

Conclusion 🤠

By writing comprehensive tests for your Room database in Jetpack Compose, you ensure the accuracy, consistency, and performance of your data operations. Testing not only validates your database schema and entity relationships but also provides confidence that changes will not disrupt existing functionality. With these steps, you can effectively test the Room database, enhancing its reliability and robustness.

--

--

Emine İNAN
Huawei Developers

Android Developer by day, Android Developer by night. @Huawei