Android ViewModel Unit Test Tutorial – Android Coding by DMTechnolab

Simplified Coding

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

So far, we have learned how to write unit tests JUnit4 And using the instrumented unit test AndroidJUnit4. We also learned Test writing for cell database.

Now, let’s start testing ViewModel.

Android ViewModel Unit Test Tutorial

I will work on the same project. And for first-timers, we’re building an application called Spend tracker. I have already made all that is needed. I will not discuss things like CreateMMelel, 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 code above, I have two tasks:

  1. To add a new expense to the database,
  2. To get the last 20 saved expenses from the database.

Much like we did when testing the room database, but this time we have these functions inside the ViewModel, and we are working in the database using the data source.

As you can see, our data source requires SpendDao, which we tested in the last post. To get SpendDao we need a database instance. Creating a database instance requires context, an Android-specific class; Therefore, we cannot perform a general unit test.

If you want to test your ViewModels using JUnit, you should try to keep your ViewModels independent of Android specific classes.

But, it is not always possible to keep ViewModels independent like this, which is why we will also learn about Roboelectric. This will help us to run such a test without using the emulator.

ViewModel test

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

The above code is much more than the last time except for one new thing

@get:Rule Val Immediate = Immediate())

We have added this rule to swap background extractor. And this new executioner will work synchronously.

Also we are using the function getOrAititValue()), And we need to define this function. This function is to get the value from LiveData.

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

Android Unit Test ViewModel TutorialAndroid Unit Test ViewModel Tutorial
Android Unit Test ViewModel

Using Roboelectric

As we are running an instrumented unit test here; And this requires an actual device or emulator. And you know that it takes time to do things on an actual device or emulator. So to speed up this process, we can use Roboelectric.

Roboelectric can only simulate Android environment in JVM. And with this, you do not need an emulator or real device to run the test. And that’s why it will make your tests much faster.

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

You can then create the same test class inside the test folder, with the following changes.

So this time we are using @run with()RobolectricTestRunner::class) Annotation for testing. We have also defined @Config()Manifested = “src / main / AndroidManifest.xml”) But it is optional here.

Now you can just run the test, but one more thing you need to do; You need to run this test using Java9.

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

Android ViewModel Unit Test Source Code

You can get the source code of my project from here.

So that’s all for this tutorial, folks. I hope you found it useful and learning something. If you have any problem or question, leave your comment below. And don’t forget to share this Android ViewModel Unit Test Tutorial with your friends. Thank you 🙂

Belal Khan ProfileBelal Khan Profile

Hi, my name is Belal Khan and I am Google Developers Specialist (GDE) for Android. A passion for teaching made me this blog. If you are an Android developer, or you are learning about Android development, I can help you a lot with simplified coding.

Source link

Be the first to comment

Leave a Reply

Your email address will not be published.