Android ViewModel Unit Test Tutorial

Hi Guys, welcome to Android ViewModel Unit Test Tutorial. This post is also part of our Android Testing Series. In this post, we will learn how to test our ViewModels.

So far, we have learned about writing Unit Tests using JUnit4 and Instrumented Unit Test using AndroidJUnit4. We also learned writing tests for Room Database.

Now, let’s begin testing ViewModel.

Android ViewModel Unit Test Tutorial

I will be working on the same project. And for first-timers, we are creating an application that is called Spend Tracker. I have already made everything that is required. I will not be discussing the things like Creating ViewModel, DataSource, etc.

You can get my source code; the link is at the bottom of this post. I have the following ViewModel in my project.

As you can see in the above code, I have two functions: 

  1. To insert a new spend into the database,
  2. To get the last 20 saved spend from the database. 

Pretty much the same as we did while testing the room database, but this time we have these functions inside ViewModel, and we are operating in the database using a DataSource. 

As you can see, our DataSource requires the SpendDao, that we tested in the last post. To get SpendDao we need the Database Instance. Building the Database Instance requires Context, an Android-specific class; hence, we cannot do a normal Unit Test.

If you want to test your ViewModels using JUnit, you must try to keep your ViewModels independent from Android Specific Classes.

But, it’s not always possible to keep ViewModels independent like this, which is why we will learn about Roboelectric as well. That will help us to run this kind of test without using an emulator.

Testing ViewModel

Now, let’s test our ViewModel. To do this again, we will generate a test class inside the androidTest package.

The code above is pretty much same as last time except one new thing that is

@get:Rule val instantTaskExecutorRule = InstantTaskExecutorRule() 

We have added this rule to swap the background executor. And this new executor will work synchronously.

Apart from this we are using the function getOrAwaitValue(), and we need to define this function. This function is to get the value from LiveData.

You can run your test but remember you need a physical device or emulator to run this test.

Android Unit Test ViewModel Tutorial
Android Unit Test ViewModel

Using Roboelectric

As we are running an instrumented unit test here; and it requires a real device or emulator. And you know it is time taking to do things on a real device or emulator. So to make this process faster, we can use Roboelectric.

Roboelectric can simulate Android Environment in JVM only. And with it, you do not need an emulator or real device to run tests. And that is why it will make your tests very fast.

To use Roboelectric, first you need to add the following dependency.

Then you can create the same test class inside test folder, with the following change.

So this time we are using @RunWith(RobolectricTestRunner::class) annotation for the test. We have also defined @Config(manifest = "src/main/AndroidManifest.xml") but it is optional here.

Now you can simply run the test, but one more thing that you need to do is; you need to run this test using Java9.

As you can see I have defined the JRE as Java9. Now you can simply run the test and it will work without an emulator.

Android ViewModel Unit Test Source Code

You can get my project’s source code from here.

So that is all for this tutorial, friends. I hope you found this helpful and learned something. In case you have any problems or questions, leave your comments below. And don’t forget to share this Android ViewModel Unit Test Tutorial with your friends. Thank You 🙂

Leave a Comment