Android Unit Test Room Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi everyone this is BELAL KHAN and you are  watching simplified coding in the last video we   learned writing unit test and our android project  and in this video we will move a little ahead   and we will learn how we can test our  room database as I told you in this   series I will be building an application and it is  a very simple application to save our spends and   the application is called spend tracker now for  this application I need to save the spend in the   local database and I will be using room to save  the database locally now I have written all the   codes that are required for room because I am not  covering room in the series this series is about   testing only now if you want to learn about  room then I have given a tutorial link that   is in the description so you can check  that out so basically in this application   I need spend to save in my database and that is  why here you can see I have created an entity   that we need to create for room database  so the table name is spends and I have   three properties for this spend and I have  one more that is our primary key that is id   so I have id date amount and description  for spam and this is my entity class   after entity I also have this spend dow now  we have these two functions that we will test   in this video so the first one is add spend  that will add a new spend to the database   and the next one is get last 20 spends now  this function will return the last 20 saved   spends from the database I also have created this  spent database class that is our abstract class   that extends or inherits the room database so as  you can see here we have the database annotation   for entities I have only one entity here that is  our spend and then we have the database version   and one more thing I have defined here that is  the type converters now here if you look at the   entity class here i'm using date so you cannot  directly save a date type to your room database   and for this date I have created this date  converter because you need to create the converter   if you want to save something to room database  that is not supported so the date is not supported   and for this I have created this date converter  and I have defined it here in my spends database   class so these all the things are already created  you can get the source code the link is given in   the description of this video what we will do in  this video is we will test our dow to see whether   it is working or not now I will open my spend  database and this time also I will right click   and i will select generate now this time we will  select test as we did in the last video because we   want to test our database so click test now this  is the class name everything is okay just hit ok   but this time save your test class inside android  test folder because to build the database context   is required that is an android specific class  and that is why we are not going to do unit test   but we will do an instrumented unit test  because we need android specific classes   and to run this test you also need a real  device or an emulator but the database test   will run faster because it is not a normal ui  test because to test database we do not need   any fragment or activity to be created and  that is why database test runs a little bit   faster than the UI tests we will learn about UI  test and coming videos so for now we will create   the test class inside this android test folder  so i'll just hit ok and this is my test class   now you need to extend test  case to your class like this   and one more thing you need to annotate your  class with run with annotation and inside this   annotation you will define android J unit this  time so we have android J unit 4 and then class   and I will also delete this J unit assert import  because we are going to use truth library for the   usher sense so we have our class now here inside  this class first I will define the database so   I will define private lead network db of type  spend database and I will also define the dao   so I have private lead in it where  dao of type spend dao like this now   I will override a function that is set up so  I will write here override fun setup like this   and I will annotate this function as before  now before means this function will be called   before each test function so whenever you  will call a test function this function   will be called before and we can have one more  function that is called after now this function   will be called after the test function so  i will create a function that is close db   so what I will do is I will create the database  inside the setup function that will be called   before the test and after the test. I will close  the database so here I will initialize my database   so what I will do is I will write db  equals to room dot in memory database   builder now this function creates a database in  memory if you will control click to this function   you can see here creates a room database builder  for an in-memory database information stored in an   in-memory database disappears when the process is  killed now this means we are basically creating a   temporary database just for the testing purpose  so we have we have called this function now this   function takes two parameters the first one is  context and the next one is the database class   now we need to get the context first and as  we are doing an android junit 4 test and it is   an instrumentation testing we can easily  get the context so to get the context we   will write here val context equals to  we will use the class that is called   application provider now with the help of this  class we will call the function get application   context and yay we have our context now we can  pass this context i need to import it as well   now we will pass this context and  then we have our database like this   so we have the database we also need to call  the build function to finally build the database   so we have the database now with the help of the  database we can get the dow so we will call the   function get spend dow and we will store it inside  dow now in the close function I will write db dot   close like this now finally we can write our  test functions now in this video I will write   only one test function so we need to annotate the  test function as test and then we will write the   function let's say I want to test fun write and  read spend because what I want to do is I want to   write and spend to the database and then I  will read that spend back and I will confirm   whether it is working fine or not so the first  thing that we need is we need the spend itself   and to create a spend we need to create an  instance of the spend so I will write spend spend   equals to spend and for the parameters we  need to pass date amount and description   so I will create a date instance  here like this I can pass the date   here let's say amount is 100 and let's say  bought something this is the description   now I will insert the spend using this dow so I  will write dow dot add spend I will pass the spin   now here you can see we are getting an error  and this is because this add span function   is a suspending function that means we need a  co routine scope to call this function because   it is a suspending function and we cannot call it  directly now if you don't know what is co routine   and how do we use co routines and our project then  you must check the co routines tutorial as well   I have given the link to the core routines  tutorial in the description of this video   so for now what I will do is I will create a  scope using the run blocking function like this   and inside run blocking we have co routine  scope and here we can call the function   suspending functions very easily so we have  inserted the spend that we have created here   now if it is working fine then when we will call  the function that is dow dot get last 20 spends   we should get the spend that we just inserted  so here i will get the spends like this   and finally I will write an assertion  so here I will write assert that.  And here I will write spends dot contains  spent I need to import the function and then   I will finally write as true now if our code is  working fine that means when we inserted the spend   we should get this spent when we  are fetching the last 20 spans   and that is why I am asserting here that  this spends should contain this spend   and it is or it should be true now we can  easily run the test and to run the test you can   click on this play button it means run all the  test functions inside this class and you can   also individually run your test functions to do  this you can click here so let's run this test   and you can see i have a real device  connected because to run these tests   you need an emulator or a real device and  oops i did a little mistake here and that is   this override fun setup should be public so I will  write public here and I will run the test again. And as you can see here the test passed  successfully that means our database is working   absolutely fine and the same way you can write  more test cases so that is all for this video   friends I hope you found this video helpful and  learn something new in case you did then please   give me a thumbs up subscribe to this channel if  you're not already a subscriber and you can share   this video with all your friends who are learning  android application development so thanks for   watching everyone this is BELAL KHAN now signing off.
Info
Channel: Simplified Coding
Views: 7,726
Rating: undefined out of 5
Keywords:
Id: Hb0UV0f6kmQ
Channel Id: undefined
Length: 12min 44sec (764 seconds)
Published: Fri Mar 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.