Android Unit Test Tutorial - Writing Your First Unit Test

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi everyone this is Belal Khan and  you are watching Simplified Coding.  From this video we will start our android  project and in this video we will write   our very first unit test using JUnit. Now here I have my android project and   this is just a blank project and the  application that I am going to build   in this course is called spend tracker. So it is very simple application we will   just enter the amount and the description and it  will save the given input to the local database.  So that we can also learn how we can test  room database or how we can write test for   room database and we will also learn a lot  of thing for example writing tests for view   model for navigation architecture and many more  things that we will see in the coming videos.  For now let's see our project structure so here  you can see whenever we create a project we get   three packages the first one is where we put all  our android code we create all the java or kotlin   classes inside this package and we also have  two more packages. One is you can see android   test we have this info or this hint within  the brackets and one is test; now these two   packages are for test code or for creating  the files that are related to testing only.  So the first one is androidTest  and here we will put all our code   related to the instrumented testing or UI  testing so basically whatever test requires   the android specific classes for example context,  fragment, activities we will put them inside this   androidTest package and then we have a test  package and here we will put all our unit testing   code so basically the raw kotlin or java code that  do not requires any android specific libraries.  So here we will put all our unit testing codes  now if you change the project structure to project   then you can see we have two folders one is named  androidTest and another one is named test only.  So when you simplify it with android you see  packages like these now in this project this   is basically an empty project and i have just  created a fragment and this fragment has a design   so we will enter the amount we will enter  the description and we will hit this button   now the first thing that we will do in this  project is we will write our very first unit test   so as i told you in the previous video we need to  write test case first and then we will refactor   our code or we will implement the code to pass  that test so basically in our test case we define   the logic for the function so let's do it so  the first thing that i will do is i will create   a object in my main package so let's create a  kotlin file class and i will name it validator   so the first thing that i will do  is i will validate the user inputs   so i will create an object and inside this object  i will create a function validate input now this   function is not yet implemented but i will write  the parameters that are required for this function   so the parameters are amount that is  end and the description that is string   now this function is not implemented but  first we will write the test case for this   function so let's create a test case for this  function what you need to do is you need to just   right click and go to generate and we will select  test now here we will use junit 4 and the class   name as validated test you can change it if you  want now everything else is fine i will just click   ok and because it is a unit test the package i  will select as just test and not the android test   so let's hit ok now this is my  class that is called validator test   now inside this class i will create a function  that will test my function that is inside this   validator object so the first thing that i will do  is i will annotate this class with run with and we   can define junit 4 because we are using junit 4  for unit testing so we will define it like this   now all the functions that you will create for  testing you will annotate it with at the rate test   like this now i will create a function let's say  fun when input is valid so i will test the case   when the input given is valid so you also need  some rules for your input for example i don't want   any amount that is 0 to be saved so if a given  amount is 0 the validation should be failed so   let's do this thing so i will write here amount  equals to 0 but this function says when input is   valid so i will write amount as let's say 100 and  description as some random description or anything   now what i will do is i will get the result so val  result equals to i will use this validator object   so validator dot validate input i will  pass amount and description like this   now we will add an assertion and one thing that i  just forget i need to use the google truth library   for assertions so we will add this because it  simplifies the assertions now we all know that   to add a library we go inside build.gradle  file so this time also we will go inside   app level build.gradle file and here you can  see we have test implementation and android test   implementations so if you define a library as test  implementation that means it will be available   inside your test package the only test package  now if you define a library as android test   implementation then it will be available inside  android test package this package now if you want   your library to be available inside both android  test and test package then you need to define   both things test implementation and android  test implementation for example i will define   the test implementation of the truth library and  i can also define the android test implementation   of this google truth library now we need to sync  the project now the truth library is available   inside both the packages now let's go to validator  test and i will remove this junit assertion import   now let's write the assertion so here we will  write assert that we will use this function and   we will pass the result and we need to import  the function that is assert that and this time   we will import the assert that function from  google truth library so you can see we have   the assert that function and then we will call  the function is equals to and we should get true   so the point is whenever we pass some  valid amount and description we should get   true from our validate input function and that  is what i defined here now to run a test you can   click on this play button and this play button  will run only this test and if you want to run   all the tests inside this class then you can  click on this play button for now let's just   run this test function so i will click on  this play button and we will run the test so you can see our test is failed and first time  always our test will be failed because we have   not even implemented the function so our test is  failing now what we will do is we will refactor   our function or we will implement our function  so that it passes our test so let's implement the   function so i will just write here return and if  amount is less than or equals to 0 or description   is empty so in this case we will return false that  means it is not valid because we want an amount   greater than 0 and a description that is not empty  and we also defined the return type as boolean now   we will run the test case again so let's go to  validator test and here we will rerun the test   and here you can see this time our test passed  now the same way i will define one more test   case when the input is invalid so when input is  invalid now this time i will pass the amount as 0   and let's also pass an empty string and this time  this validate input function should return false   so we will write assert that result is equals to  false like this now we will run the test again and you can see the test is passed so this  is how we create unit tests for our functions   so i think that is all for this video friends  and in the next video we will move ahead with   this project we will create our database we will  test the database and we will do a lot more things   so in case you have found this video helpful  and learned something you can give me a like   and if you're not already a subscriber  then please hit on that subscribe button   and subscribe this channel you can  also click on that bell icon to get   the notification updates so thanks for watching  everyone this is Belal Khan now signing off.
Info
Channel: Simplified Coding
Views: 26,225
Rating: undefined out of 5
Keywords:
Id: rM25aJ0o3Us
Channel Id: undefined
Length: 11min 18sec (678 seconds)
Published: Sun Feb 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.