Now i am creating a tutorial on SQLite with Unit Tests in Android. Here i am using Android Studio Beta 0.8 as an IDE.
When mostly Android developers start working with SQLite, they waste a huge amount of time by testing “is DB created?” by launching Emulator or using Rooted Device. After that they open DDMS and go to file explorer and move one by one from directory structure: data /data / package-name/ databases/…..
Some sharp devs use terminal but again waste there time using commands like:
1. adb shell
Result nothing again change some file and again repeat all steps.
And the great thing happen when after a some time we change some schema or add new table we again waste our time.
0. Now we are creating an app in which we create a Simple DB with name school.db and create a single table with name Student.
Our table schema looks like:
1. Create a Project on Android Studio:
2. Create a new package with name db:
3. Create two new files one is DBContract and the second is DBHelper:
3.1. Now discussion about DBContract file:
When creating DB in any application, always create a File with name of Contract, where add all of your table’s meta data in static classes like Table name, Column names and when we move to ContentProvider at that time we also discuss to add CONTENT_URI and CONTENT_TYPE etc here. Sorry it may be getting difficult so for the time being forgot about ContentProvider and next text :).
So simple I think we did not need to explain above class. Only we have some constants.
3.2 – DBHelper class
Now focus on the name of the class. Name is DBHelper.
Now DB mean database and Helper mean any thing which help us to resolve some issue. Here Helper means who help us to resolve our DB operations.
Now see the below image of our DBHelper.java class.
If you see on line 13 we extends our DBHelper from SQLiteOpenHelper. After that we got two abstract methods which we define onCreate and onUpgrade.
In onCreate we only create our table using SQL query. So when App run first time the onCreate method automatically call and create our DB and Table. onUpgrade method is used when we update our app but we explain that method in some other tutorial 🙂 .
Now we are ready to check is our DB working.
4. Android Unit Test For SQLite Testing
As you see directory structure. We have one folder with Name androidTest. Now i am creating a new UnitTest file in this directory with name DBTesting.java
Now start disscussion on this DBTesting file.
First if you see on line 14 we extend our class from AndroidTestCase. So simple now we are ready to write our tests.
After that if you see on line 23 here i write my first test which is testDropDB . Where i only test to drop my Database if exist.
One more thing any method you want to make a test append test as prefix with that method name.
Now second method is testCreateDB if that pass its mean our DB creation is working properly. OK now one more small thing how we run our tests.
So for that we create a new configuration for running test.
Now click on the buttons where we show arrow in blow images.
Now to run our test select our created configuration.
Now press Run and magic happens. Result you can see in Android Studio as shown below.
Hurray our test pass:
Now one more image if test fail at that time what things Android Studio show us:
One question can you tell me why our test fail.
OK i am telling, i swap the lines 31 and 32 so that’s why our test fail.
Now i am writing two more tests for inserting and retrieving data.
Code available at : Githubby
9 thoughts on “Android Easy SQLite With Unit Tests”
Thanks so much for the article post.Much thanks again. Want more.
I am using Godaddy.com
HI, thank you like a million time 🙂 that is really helped me a lot 🙂 🙂 you are great 🙂
Well done aritcle that. I’ll make sure to use it wisely.
Thanks for appreciation.
Really enjoyed this blog. Will read on…
what is mContext”?
mContext is an Android Context available for test cases. We are getting from AndroidTestCase base class.