Room with Unit test in Kotlin
As google rolled out its new data persistence lib Room in I/O 2017 and a really cool thing LiveData. I just could not wait to get my hands on it. Wait…, am i forgetting something (:thinking:) : Yes, “Kotlin”. A really cool programming language leveraging modern programming approach.
So now lets dive into it.
If you want to do hands on along with this article, clone this github repository .
git clone https://github.com/chandilsachin/Room-with-unit_test_demo.git
Prerequisites:
- Android Studio 3.0 Canary 8
Step 1: Adding dependencies:
Put following lines into build.gradle(Project: app_name)
Add following lines into your build.gradle(Module: app)
Now lets create an Entity class. It is referred as a database table.
Step 2: Setting up database
Creating a constant file to help with constants
Entity class: entity class is also referred as tables.
@ColumnInfo
can be used to specify a column name for the field or change the column’s type affinity. Don’t forget to assign default values in constructor otherwise app build will throw exceptions.
DAO class: Data Access Object class that is used to write your queries.
Here all database operations comes. This is where all crux of Room lib resides. Annotations are used to generate boilerplate code for you, that we needed to write every time we want to write an operation with SQLiteOpenHelper
class.
@Query
annotation is used to write sql queries that return data. In kotlin passing parameter is bit different than Java. Kotlin uses arg0
, arg1
, …, argN
placeholders to reference method parameters with respect to sequence.
@Insert
is used to insert data in database table. Properties of object that being passed in insert method should match the column in database table(Properties in Food.kt entity class).
@Update
update also works same way as insert.
@Delete
deletes row in table. To delete row(s), it matches a property or properties(In case of candidate key) specified as primary key. To delete all rows in table @Query annotation can be used. check flushFoodData()
method in above DAO class.
I recommend to read Android Room persistence article to know more on how to write such queries.
Database class: This class puts everything together and sets up database.
This is singleton database class. Notice that I have created a condition in getInstance() method to easily get database instance for testing.
Step 3: invoking database operation:
doAsync {
val list = ArrayList<Food>()
list.add(Food(1, "Egg", ""))
list.add(Food(2, "Banana", ""))
list.add(Food(3, "Milk", ""))
list.add(Food(4, "Cashew", ""))
list.add(Food(5, "Almonds", ""))
FoodDatabase.getInstance(context).insertFoodList(list)
// or
FoodDatabase.getInstance(context).insertFood(Food(1, "Egg", ""))
....
....
}
like this you can call methods written in DAO class.
Step 4: Setting up Unit test:
Now lets dive into Instrumentation Unit testing for Room database
Unit test class
Create a test class in src/androidTest/java
with corresponding package of your database class.
In `setup()` method, DOA instance is created to do operation in test methods. Notice first line in setup method, TEST_MODE
constant is set to true to get database instance for testing mode. It creates a temporary database that does not touch our main database in the app and flushes that database when all test cases are completed. So no more handling of temporary database and deleting them after cases finished running.
Now lets run test: click on the database class in Android project explorer and click on run ‘test_class_name’.
This is the basic blog to showcase basic functionality. I will be creating another blog to explore it in more details in Part II.
I hope it is helpful for readers. Let me know your suggestion, improvement, correction if any.
Thanks.