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:

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:

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.

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:

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.

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.

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.

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.

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 🙂