Painless Android testing with Room & Koin
Google’s Android Architecture Components is a very powerful API to help us build smarter applications. In my previous article, we talked about ViewModel components and how the Koin container help makes dependency injection directly through ViewModel classes’ constructor.
Unlock your Android ViewModel power with Koin
Google has made a very nice gift with its Android Architecture Components. Now we have in hands tools and power to…
Now, let’s see how we can easily test Room persistence with Koin👌
TL;DR: Koin dependency injection help you retrieve your components directly in your AndroidTest. Room components declaration and testing is also quite straightforward. Enjoy it!
Starting Room components with Koin
Let’s start with a simple app that need to store weather data. For this, we use the following Room entity:
To use our WeatherEntity data, let’s define a Room DAO and a Room Database:
Check that your are using one the following Koin Android project:
Let’s assemble all things with a Koin module and declare our DAO & Database instances.
✅ The module consists in “bean” expression (singleton declaration) for each Room part. We reuse the WeatherDatabase instance to declare our WeatherDAO instance:
✅ Our app is now ready to inject our WeatherDAO in any class constructor👍
We can make a repository class and inject it by constructor:
Bring testing features
For testing our Room DAO component, we need to retrieve our DAO instance in a test class. For this, just add the following Gradle dependency:
✅ Koin-test project offers the KoinTest interface with its inject() and get() instance retrieving functions.
✅ In AndroidTest, your test class is started with this Instrumented Application context and your default Koin configuration is available.
Testing our DAO & more ✨
Let’s write a WeatherDAOTest file and tag it with KoinTest interface, to later retrieve our Room components.
⚠️ Google suggests to test Room components via AndroidTest to ensure that your are using the right version of SQLLite under the hood.
✅ Let’s write a Koin module to provide an In-Memory Room database, in order to not impact database from our device (and ensure “replayable” tests):
Because we use the Koin container to make dependency injection, we just need to define a bean for replacing the default Room database instance with InMemory one.
All we need is to load this new definition with its module via loadKoinModules() function (it overloads default WeatherDatabase definition with the InMemory one). That’s just it 👍. Now… just use your DAO and check the requests:
The testSave() test produce data and use the WeatherDAO to store it. We can check that all has been clearly stored 🎯
We can write the same kind of test, but for or WeatherRepository component. It can be interesting to make an integration test.
Assembling and testing Room components with Koin, is one part of the “Android Architecture & Architecture Components” workshop that we gave at AndroidMakers with Laurent BARESSE and Florina Muntenescu.