Android – Testing LiveData and ViewModel effectively

Tram Ho

I. Opening

The Architecture Components were introduced on Google I / O 2017 and they mean a significant improvement in the Android development world. There are also many articles about the Architecture Components and how to implement them. One of the benefits of implementing them is that it’s easier to test. But how to understand the ability to test. Let’s go into the article today.

II. View and ViewModel communicate via LiveData

One of the key measures of the Architecture Components is the Pattern Observer to update the Activity or Fragment and handle common problems. You can refer to some of these articles to better understand: https://viblo.asia/p/android-architecture-giao-tiep-giua-viewmodelpresenter-va-view-Ljy5VLN3Zra

The important thing here is that the View is still passive, keeping all the logic in the ViewModel, and sending user actions immediately to the ViewModel. The complexity for verification by testing is still in the ViewModel and if possible, we should avoid relying on android. * to be able to test using pure JUnit test. This is very important to achieve an effective ViewModel Test

III. Effective ViewModel test

Some goals want to achieve

  1. Perform quick check
  2. Modeling the actual use cases of the ViewModel being tested
  3. Easy to write test and easy to maintain

Resolving the first request means keeping the test in JUnit only, without using a Robolectric test or device. This can easily be achieved and does not take much effort.

At the second requirement, we pay attention to why the Architecture Components provide improvements for test capabilities. Splitting components makes it easier to replace Activity with another. Testing can function normally as Activity while keeping the actual test model used to interact with the ViewModel.

We will now discuss the third goal

IV. Write effective ViewModel test

In the first introduction about LiveData and ViewModel, the question was asked: Is this RxJava? It is not, but obviously there are some things in common.

Having this in mind, along with the fact that RxJava is a very well tested library, we can take inspiration from the test method there. Each type has a test () method that returns the TestObserver or TestSubscacker to perform check verifications ok

We can deploy our own TestObserver for LiveData with validation requests, because currently we use tools such as Kotlin’s extension methods, we can achieve the same comfort as RxJava. to have test () method on LiveData directly. For example:

Doing this uses a simple extension method to register the LiveData and return the TestObserver

The TestObserver is Java class, so you can use this approach even if your project only uses Java without Kotlin. The only difference will be to call the static method:

Please note that all tests using existing LiveData must include InstantTaskExecutorRule from android.arch.core: core-testing

V. Summary

Has such a writing test been effective? It can be said that the 3 goals mentioned above can be met. Thank you for following the article. source: https://android.jlelse.eu/effective-livedata-and-viewmodel-testing-17f25069fcd4?fbclid=IwAR2Gz5kubbH92BQ4sPiJv8OmW_lvSAd0XXq9DSlqdQxeI5ZCKDwBauQcwqY

Share the news now

Source : Viblo