Unit Testing for beginners in Android

Abhishek Pathak
3 min readOct 13, 2022

--

Unit testing is a testing methodology where we test individual functionality (cover all the edge cases of each small feature) of application code, to ensure that code written must be work as expected.

Why Unit testing is important?

Unit testing is one of the software testing types which includes the initial testing phase where the smallest components or the modules of a software are tested individually.

With each method of testing, both testers and developers can isolate each module, identify and fix the system defects at a very early stage of the software development lifecycle. This can be via BDD or TDD.

A unit is known as the smallest possible component of software that can be tested.

Primarily, a unit test verifies different behavioral aspects of the system under test and can be broadly classified into state-based and interaction-based unit testing.

Android testing Triangle concept

Testing triangle is a way to segregate the unit testing for whole app by their requirement like UI’s should be tested using Espresso, Robo electric test run. Another classes like ViewModels, repositories, helpers, presenters, utils should be run via Junit/Mockito/Mockk local testing. While classes that are using android processes like Room Database that is require to pass context to instantiate will go under Instrumentation/Integration testing.

  • Unit Testing : This is also called as local testing. Junit and Mockito library can be used to test classes like ViewModel if using MVVM, Presenter if using MVP, repository if using repository pattern, Helpers/ Utils etc. These tests run using local JVM so the speed of running the test case is faster among other testing ways. These testing require dependencies of library Junit and Mockito. These test placed under (test) dir.
  • Integration testing : This is a way of testing the classes which is using context or any android process so that require an android process like emulator to run the test. Example like room DB require context so it can be tested under integration testing and these tests placed under (android test) dir. These testing require android emulator to run the test cases so it is slower than local testing but faster than UI testing. These testing require dependencies of library Junit and Mockito.
  • UI Test: This is a kind of testing where we can test our UI’s of Android app like activities and fragments. We need Espresso library to run these test. UI tests run over emulator or real device so these are slowest in terms of speed of testing. These tests placed under (android test) dir.

Benefits of Unit Testing

  • Unit Testing helps in finding bugs early stage rather than getting bugs after deployment of app and making bad impact on impression of application.
  • Unit Testing reduces the testing efforts because all the scenario’s is covered and then there is very less time required to check functionality.
  • Unit testing simplifies refactoring and provides documentation because each scenario must be on a documented form to make all team on same page.
  • Unit testing insures the quality of code.

Unit testing implementation series

Thanks for reading this article. Be sure to click 👏 below to applause this article if you found it helpful. It means a lot to me.

--

--