Testing LiveData on Android

Reading Time: 3 minutes

Testing LiveData represents an interesting challenge due to the peculiarities of its technology and the way it eases development for your Android app.

I’ve recently started to build an Android app to keep motivated on my journey to learn Kotlin. My most recent experience has been with Architecture Components and this brief blog post, in particular, will focus on unit testing your DAO when using LiveData.

What is LiveData?

LiveData is a lifecycle-aware, observable data holder that will help you react to changes in you data source. In my case, I’m using it in combination with Room to make sure my app reacts to the new data becoming available in my database.

Our simple DAO

For this blog post let’s just pretend we have a very simple DAO that looks like the following:

This is just a DAO that helps you fetch posts.

As you can see, the return type for the function is not just a plain List<Post> but it wraps it in a LiveData instance. This is great because we can get an instance of our list of posts once and then observe for changes and react to them.

Let’s test it

The Android Developer documentation has a neat example on how to unit test your DAO:

This pretty simple test has the aim of testing that the data is being exposed correctly and that, once the post is added to the database, it is reflected by the getAll() invocation.

Unfortunately, by the time we are asserting on it, the value of the LiveData instance will not be populated and will make our test fail. This is because LiveData uses a lifecycle-oriented asynchronous mechanism to populate the underlying data and expects an observer to be registered in order to inform about data changes.

Observe your data

LiveData offers a convenient observe method that allows for observing the data as it changes. We can use it to register an observer that will assert on the expected value.

The observe method has the following signature:

void observe(LifecycleOwner owner, Observer<T> observer)

It expects an observer instance and the owner for its life-cycle. In our case, we’re only interested in keeping the observer around just enough to be able to assert on the changed data. We don’t want the same assertion to be evaluated every time the data changes.

Own your lifecycle

What we can do, then, is build an observer instance that owns its own life-cycle. After handling the onChange event we will mark the observer life-cycle as destroyed and let the framework do the rest.

Let’s see what the observer code looks like:

This observer implementation accepts a lambda that will be executed as part of the onChange event. As soon as the handler is complete, its own lifecycle will proceed to mark itself as ON_DESTROY which will trigger the removal process from the LiveData instance.

We can then create an extension on LiveData to leverage this kind of observer:

fun <T> LiveData<T>.observeOnce(onChangeHandler: (T) -> Unit) { 
    val observer = OneTimeObserver(handler = onChangeHandler) 
    observe(observer, observer)
}

Let’s test it again

A couple of things to notice this time.

First of all, we’re taking advantage of an InstantTaskExecutorRule. This is a helpful utility rule that takes care of swapping the background asynchronous task executor with a synchronous one. This is vital to be able to deterministically test our code. (Check this out if you wanna know more about JUnit rules).

In addition to that, we’re now leveraging the LiveData extension that we have written to write our assertions:

postDao.getAll().observeOnce {
    assertEquals(0, it.size)
}

We have just asserted in a much more compact and expressive way by leaving all the details inside our observer implementation. We are now asserting on the LiveData instance in a deterministic way making this kind of tests easier to read and write.

Conclusion

I hope this post will help you write tests for your DAO more effectively. This is one of my earliest experiences with Kotlin and Android: please, feel free to comment with better approaches to solve this.

Follow me on Twitter for more!

Cover photo by Irvan Smith on Unsplash


Posted

in

,

by

Comments

7 responses to “Testing LiveData on Android”

  1. Natthawut Hemathulin avatar
    Natthawut Hemathulin

    Thank a lot, you save my day. 😂

  2. X4257 avatar

    Very clear, thanks

  3. deepakbaliga avatar

    Thank you very much @alediaferia. Beautiful work

  4. Mike Szczys avatar

    Superb, exactly what I needed for my Room database testing. Thanks!

  5. Mook avatar
    Mook

    Thank you for this! It worked well.

    Since I was already in a `suspend fun`, I combined this with a `CompletableDeferred` to turn this into `suspend fun LiveData.await(): T`, which means the testing code can read like synchronous code instead. I did have to wrap the `ON_DESTROY` call in a `runOnUiThread`, though; apparently the observer must be removed from the main thread.

    Hope that helps! I have no confidence in maintaining the formatting, hence not posting the whole thing; hopefully that’s enough description to work out what I did.

  6. arezaiyan avatar

    Thank you,

    For the cases you need more than 1 time observation, I’ve changed your implementation to support asserting multiple states.

    Here you can see: https://gist.github.com/rezaiyan/e8443918ddf8ac5b60e2555e23460e93

  7. Neill Barrett avatar
    Neill Barrett

    I tried to follow your suggestion of putting the extension “`fun LiveData.observeOnce(onChangeHandler: (T) -> Unit) “` and its contents in the same file as “`class OneTimeObserver(private val handler: (T) -> Unit)“`. Android Studio highlights each ‘observer’ in “`observe(observer, observer)“` and says “unresolved reference: observer”. How do I fix that?

Leave a Reply

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