Android Architecture Components: Testing your Room DAO classes

In this previous post, we look at testing our ViewModel LiveData from the classes used in the Architecture components library. You can check this post out here if you haven’t yet:

https://medium.com/exploring-android/android-architecture-components-testing-your-viewmodel-livedata-70177af89c6e

Today we’re here to talk about how we can write tests for our DAO classes when we’re using Room for our Database layer in our application. The great thing is is that this is a straight forward concept and is probably similar (if not the same) to approaches you are already testing in your applications!


To begin with, we’re going to need a DAO class to reference when we’re talking about these tests that we’re going to write. I’m going to go ahead and take this one from the Buffer Android boilerplate:

https://medium.com/exploring-android/android-architecture-components-testing-your-viewmodel-livedata-70177af89c6e

For this example we’re going to be using the CachedBufferooDao class, this is a DAO that we use to simply get, insert and delete instances of the bufferoo class from the project.

https://gist.github.com/hitherejoe/55581826e7aed71cff7b4ca55585d1f7

You can see in our DAO we have three abstract functions which are used to manipulate the data stored in our Room database. We want to write to tests to ensure that the this class and the queries used remain maintainable, so let’s get started!


To begin with, we’re going to write these as Instrumented tests using the AndroidJUnit4 test class. Before we run through this class, this is what our complete class could look like:

https://gist.github.com/hitherejoe/6319d092fc110ab2a352be19f52b8884

Initialising the database

We begin in the @Before function by creating an in memory instance of our Room database. The difference with the in-memory database is that any data that has been added to the database will be cleared once the process is killed, which makes it perfect for using during tests. We simply pass in a reference to our context and the database class which is to be used for the builder.

https://gist.github.com/hitherejoe/56aa27bc0707a2a5327fbf800b00abb2

Testing the insertBufferoos() function

This function handles the saving of our entities to the room database. So because of this, we want to go ahead and write a test to ensure that the functionality of this function remains intact.

https://gist.github.com/hitherejoe/8c806cbc2ad2ca6f3186b9e87191924d

Here we simply use the instance of the bufferoosDatabase that we created to access the DAO instance that we are testing. From here, we use the insertBufferoo() method for the DAO to insert our entity instance.

Now, we’re going to use the getBufferoos() method of the DAO to retrieve the stored bufferoos to assert that the data we inserted was saved. We do this here by checking that bufferoos.isNotEmpty().

Note: Some people will prefer to write pure SQL methods when performing operations that aren’t related to the test that you are carrying out. This does help the focus of your test methods but to keep things simpler here I haven’t done so.

Testing the getBufferoos() function

This function handles the retrieval of our entities from the room database. Because of this, we want to go ahead and write a test to ensure that the functionality of this function remains intact.

https://gist.github.com/hitherejoe/65844085907e68bbdab85b99c85c7a06

Here we again use the instance of the bufferoosDatabase and from here, we use the insertBufferoo() method for the DAO to insert our entity instances that we need in the database to test the retrieval function.

Next, we’re going to use the getBufferoos() method of the DAO to retrieve the stored bufferoos to assert that the data we inserted is returned by this function. We do this here by checking that the data we retrieve is the same as the list we originally inserted.

Testing the clearBufferoos() function

This function handles the clearing of our entities from the room database. We are going to write a test to ensure that the functionality of this function remains intact.

https://gist.github.com/hitherejoe/233230f6bea721e5a22414dd91b6599c

Here we again use the instance of the bufferoosDatabase and from here, we use the insertBufferoo() method for the DAO to insert our entity instances that we need in the database to test the retrieval function.

Next, we’re going to use the clearBufferoos() method of the DAO to clear the bufferoo entities from the database. Now want to check that this worked, so we co ahead and make use of the getBufferoos() DAO function to retrieve any stored entities from the database. We then check that the returned list is empty to ensure that the data was cleared.


The above methods are a simple introduction to how you can write unit tests for your DAO classes. I’m looking forward to sharing more to do with the testing of architecture components as I learn more 🙂

https://medium.com/exploring-android/android-architecture-components-testing-your-viewmodel-livedata-70177af89c6e

Leave a Reply

Your email address will not be published. Required fields are marked *