Android Unit Test Room Database – Android Coding by DMTechnolab

Simplified Coding


In the previous post we learned Writing the first unit test In our Android project. Now the time has come to level things a little. The title of this post is “Android Unit Test Room Database”; We will focus on testing the database of rooms this time.

I think what you usually do is you write your database code, and then you manually check the database inspector to see if your code is working. It works, but the method is prone to errors, and that’s why we should write a unit test to check all our database functions.

Create a simple database

For the unit test room database, we need a database. Let’s create a straightforward database to test quickly. You can also refer to it Android room tutorial To learn about the database of the room in detail.

power

As we discussed in the previous post, we are building an application named “Spend tracker” Throughout this Android test tutorial series. The data class below is our unit for spending in the cell database.

As we are using a date type here that is not supported by the room; We need to create a TypeConverter.

Dao

To perform database operations we need to create a Dow interface.

The database

Finally we create our database.

Our database has been created and now we can perform operations for this database. Now test our database to make sure it is expected.

Android Unit Test Room Database

As we did in the previous post, we will generate a test class. The process is very simple. Right click anywhere inside your class that you want to test. In this case it is SpendDatabase.

Now choose from the options Generate …

Android Unit Test Room DatabaseAndroid Unit Test Room Database
Android Unit Test Room Database

After selecting Generate, you have to select Test.

examexam
exam

Now you will see another popup window, which has options for your test class, you just don’t need to change anything in Hit OK.

Test makingTest making
Test making

When you hit OK, a new dialog will ask you to select the package for your test class; Make sure you choose AndroidTest Give the package this time.

We are creating a test class inside AndroidTest because we will be testing Android-specific stuff, and so it is not a regular unit test, but an instrumented unit test. An emulator or actual device will also be required to run this test.

We can also convert this test to a unit test using Roboelectric, as Roboelectric can simulate an Android environment using only JVM. But we will learn about it in a separate post. For now, let’s do the Instrumented Unit Test.

Make unit test

You have to write the following code, inside the test class that we have just generated.

This time you can see that we are using @run with()AndroidJUnit4::class). Different @exam Work, at this time, we also have the following.

@before this: The function marked with this annotation is executed before each test.
@After: The function marked with this annotation is executed after each test.

As you can see in @before this, We are creating databases to test and in. @After, We are closing the database.
Finally, inside @exam Function, we spend first in the database, and then we are getting the last 20 spend. If our code is correct and is working fine, then the 20 spent should be what we have just incurred. And we are claiming to be the same in the last line.

Now you can run the test.

Android unit test room database tutorialAndroid unit test room database tutorial
Android Unit Test Room Database

As you can see our test has passed. In the same way you can fix more test cases.

challenge you

congratulations! Upon completion of this post. Now here is a challenge for you. In this post, we created a category Datacontroller. It would be best to test classroom tasks. And your challenge is to write unit test (same as last post).

You can comment your answers below.

Android Unit Test Room Database Source Code

Finally if you need the source code of my project you can get it from here.

So this is all for this post guys. I hope you can do the unit testing room database now. If you have any questions about this post, comment below. And finally, make sure you share this post with your friends. Thank you 🙂

Belal Khan ProfileBelal Khan Profile

Hi, my name is Belal Khan and I am Google Developers Specialist (GDE) for Android. A passion for teaching made me this blog. If you are an Android developer, or you are learning about Android development, I can help you a lot with simplified coding.



Source link

1 Comment

  1. Thanks for a marvelous posting! I certainly enjoyed reading it, you might be a
    great author. I will make sure to bookmark your blog and will eventually come back very soon. I want to encourage you continue
    your great writing, have a nice afternoon!

Leave a Reply

Your email address will not be published.


*