Android Easy SQLite With Unit Tests

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.

Motivation:

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

2. .dump

3. …………..

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.

Start:

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:

DB Schema

DB Schema

1. Create a Project on Android Studio:

Android Studio Project

2.  Create a new package with name db:

New package db

3. Create two new files one is DBContract and the second is DBHelper:

New files DBContract and 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 :).

DBContract.class

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.

DBHelper.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

Project directory structure

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

Unit Test Directory Structure

Now start disscussion on this DBTesting file.

DBTesting.java

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.

Waiting ……………

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 : Github

Facebooktwitterredditpinterestlinkedinmailby feather

9 thoughts on “Android Easy SQLite With Unit Tests

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.