Android ViewModel Unit Test Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi everyone this is BELAL KHAN and you are  watching simplified coding in this video we   will learn testing our few models and as you  can see in my project I have created a new   spend view model this span view model takes the  spends tracker data source and this data source   we created in the last video using our spend dao  so this data source has two functions one is to   add a new span to the database and another one  is to get the last 20 spans from the database   now inside the view model as well I have  only two functions one is to add a spend   and next one is to get the last 20 spends and one  more thing I have in my view model that does this   last 20 spends live data so when we will fetch  the last 20 sprints from the database we will   observe the data using this live data now we need  to test this thing and to test it again we will do   the same thing we will create a test class and  this time also we need to create a test class   inside android test folder because our view  model requires the spend stack a data source that   requires our database and to create the database  we need context so what I will do is I will right   click on the spend view model class I will go  to generate then I will select test now I will   create spend view model test now let's hit ok and  this time we will select the android test folder   now we have our test class ready and again I will  delete this JUnit assert import because we are not   using it and again we need to annotate this class  using run with and we will use the android JUnit 4   class like this and we also need to extend test  case to our class because it is a test class   now here inside this class I want to test my  view model so first I will define a private   lead netware that is my view model so the type  is span view model now I will override the setup   functions so we have public override fun setup  so let's write it here public override fund setup   and this is the function now inside this function  we will set up the test that means we will create   the viewmodel and we will create everything that  is required to create our viewmodel now you can   see to create viewmodel we need this spend stacker  data source and to create the spend tracker data   source we need our spandau and for the spandau we  need our database so let's go to viewmodel test   and here first I will create the database but to  create the database we need context so the first   thing that we will get here is the context  so first we will get the context like this   and then we will create our database  so i'll create db equals to room   dot n memory database builder the first parameter  is context the next parameter is the database   itself so here I will pass spend database. Class dot java then finally we will   build the database but this time I will also  call allow main thread queries now I have   defined here allow main thread queries because  I do not want to switch threads just for testing   and that is why I have defined that allow me  in thread queries now using the db we can get   our spend stacker data source so we will write  here val data source equals to spends tracker   data source and it takes our dow and we can get  the dao from the db so we have the data source now   now we can create our view model so here I will  write view model equals to spends view model.  Like this and we will pass the data  source that's it so we have the view model   I also need to annotate this function as before  because I want to run it before every test   now we will create our test function that we will  annotate with test and finally I will create here   a function that is called fun test view or spend  view model you can name your function anything   that you want so this is my function that will  test my view model now inside my view model I am   adding a spend and I am getting the last 20 spans  now this time also I will do the same thing as we   did in the previous video I will add a spend using  the view model and then we will fetch the last 20   spans using viewmodel and then we will check if  the added spend exists or not in the last 20 spans   so let's do it now we have added the spent after  adding the spend we will call the function that is   get last 20 spends now we need to check if our  added spend exists in the last 20 spans or not   now to get the last 20 spans from view model  what we will do is we will call our live data   so we have the last 20 spans now we  need to get value from this live data   and to do this thing we need to create one more  separate function so I got this function from you   know sample code but I will write the code just  to make sure that you understand the concept so   inside my android test package I will create a  new file and I will name it live data test utils   or you can name it anything that you want we don't  need a class we just need to create a function   and it will be an extension function of live  data so here i will create live data of type t   dot get or await value because here in case of  live data we need to wait until it gets the value   and we need to define a generic function that  will work for all live data so that is why i will   define here type as t so we have our function that  is get or await value now the first thing here   that we will do is we will define a data var so we  have var data of type t and the initial value as   null and we will return this t from this  function so we will return data as t like this   and we also need to define the return type for  this function that is t now here we need to wait   until our live data gets the value and to  wait for it i will use a countdown latch   that we use for waiting for other threads to  complete so here i will create a latch let's   say we have latch equals to countdown latch  and i want to wait just for one thread so   whenever we will get our observer called and  we will get the live data value we will stop   waiting with the help of this latch now here  I will write latch dot of weight like this   now this latch will wait until the live data gets  the value but there may be cases when our live   data will never get the value and to solve this  issue I will wait for 2 seconds only so I will   define here 2 and for the time unit I will define  seconds so I have time unit dot seconds here   now in case our live data never gets the value  i will throw an exception so here I will throw   time out exception live data never gets its value  or you can write whatever message you want so   that's it now finally we will create an observer  so let's create an observer here I will create   val observer equals to observer and we need to  create and as an anonymous inner class so we will   use object and then not live data but observer of  type t like this and inside the observer we will   implement the function that is on changed I think  we don't need this parenthesis here like this   now inside the unchanged function we will get the  live data value and we will assign this value to   our data so here I will write data equals to t  like this and then we will remove the observer   so here I will write this at the rate get or await  value dot remove observer and we will pass this   that is our current observer and after this we  will call the countdown function in our latch   so that it will stop waiting for the live data  value so here we will write latch dot countdown   like this now finally we will attach this  observer to our live data so here we will   write this dot observe forever because this is our  live data and we will put our observer like this   now finally if we throw this timeout exception  then also we will remove the observer so we have   try and finally inside finally we will remove the  observer so we will remove the observer like this   and that's it our function to test live data  is ready and now we can use this function to   get the value from the live data so here we  will come back to our ViewModel test and here   we will call the function get or await  value now this function will give us the   live data value and it is a list of spends  and from the list we can call find function   and inside find function we can  define it equals to equals to   not at actually at dot amount equals to equals  to 170 and it dot description equals to equals to   bot or the same string so i will copy paste  just to make sure I won't do any mistake   so if this add function worked  successfully we will get the added   spend inside this last 20 spans live data and we  will store the found element inside a result now   finally we can assert that as assert that  result not equals to null and it is true   we need to import the assert that function.  And that's it so we are asserting that  result is not equals to null because   if we found something like this we will  get some result if not we will get null   so if the result is not null that means our code  is working absolutely fine now our test function   is ready but before running the test I need to  define a rule so here we will define a rule inside   our test class and that is to define a  rule we use at the rate get and then rule   like this and I will define here val instant task  executor true and it is instant task executor rule   like this now if you don't know what is  this then it is a junit test rule that   swaps the background executor used by architecture  components and it swaps it with a different one   which executes each task synchronously so for  testing I want to execute everything synchronously   because it is required so that is why I have  defined this rule here now let's run the test.  And make sure you have connected to a real device  or an emulator because to run this test a real   device or an emulator is required so the tests  are running and you can see the test passed so   it is working absolutely fine our test passed  that means the view model is working as expected   so that is how you can test view models your view  models and you can write as many test cases as you   want or as required so that is all for this video  friends in case you want the source code then you   can get it from the link that is given in the  description of this video and I hope you found   this video helpful and learn something new so if  you found this video helpful 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 so thanks for watching  everyone this is BELAL KHAN now signing off
Info
Channel: Simplified Coding
Views: 23,237
Rating: undefined out of 5
Keywords: mvvm unit testing, android viewmodel unit test, android unit test tutorial, test driven development, android unit testing, android unit test viewmodel livedata, android unit test live data
Id: mNUBSRmj0Uk
Channel Id: undefined
Length: 14min 58sec (898 seconds)
Published: Fri Mar 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.