How To Unit Test Android ViewModel With LiveData & Kotlin Coroutines



In this blog posts, I will show you a project that demonstrate unit testing for ViewModel with LiveData with Kotlin Coroutines that follows a basic MVVM or MVI Architecture. We will write a unit tests for a ViewModel that makes a network call to validate if its behaving as expected or not.

The implementation is already in my Bitbucket. Checkout this commit 

You should go through the project and come back here. The project follows a basic MVVM architecture and has the full unit test implementation.

Basically the unit tests mock, spy and observe the object and its dependencies and we'll use assertions and verifications methods. We never really uses production data in unit-tests. We never really uses actual objects in unit tests.

Unit tests as its name implies `unit` is a test of smaller parts of a large system.

Your project must be testable to begin with. The objects or classes in your system must be mockable. If not you should do some re-work.

You can do some research on how to write testable codes. The project I linked in this blog posts itself demonstrates a testable code. You should checkout every links on that project. Go through it thoroughly.

Thanks for reading.

Comments

Popular posts from this blog

How To Check If Your Android Phone Is Official Or Rooted

Android Reverse Engineering - Beginners Guide To Smali Coding

Conquering macOS Upgrades: A Guide for iOS App Developers