Better Programming

Advice for programmers.

Follow publication

Member-only story

Android Unit Testing Basics

Satya Pavan Kantamani
Better Programming
Published in
7 min readDec 19, 2021

Image by author

In mobile application development, we have many trending things which are in progress like MVVM, Jetpack, KMM(Kotlin Multiplatform), etc. Testing is one of the important and preferred things nowadays. Testing has become a crucial factor in development. There are multiple strategies for app testing. In most of the interviews, the knowledge of writing unit test cases has become a must. So in this post let’s see what is unit testing and some basics of writing local unit test cases.

As everything can’t be covered in a single post I will be writing a series on testing. If you want to jump to code please check out the Github repo: android_unit_tests

Do we need to write Unit Test Cases?

The answer would be yes to most of the cases. As we have faster development cycles running to meet product requirements we are out of this scope in most cases or not adopting it as we haven’t started. However, out there many companies are following the approach of writing unit tests for achieving better code quality in order to build great products.

Even writing unit tests helps to resolve bugs and on the other hand, writing test cases can also help to resolve issues related to existing code changes of which we might not be aware.

What is Unit Testing?

Unit testing means testing the written code by small parts. We usually write programs that have classes and further the logic in classes is divided into methods. Unit refers to a small piece of code that can be either method, class, or component. The aim of unit tests is to be to verify the logic of individual units.

How to write Simple Unit Test

We use the JUnit framework to write simple unit tests. Let’s see how to write a simple unit test for a method that validates the given email. For this first, we need to know where to write these tests.

We usually have a project structure in Android Studio where we use

  • The main folder for writing the application logic.
  • The test folder is used for writing unit tests.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Satya Pavan Kantamani
Satya Pavan Kantamani

Written by Satya Pavan Kantamani

Android Dev, Interested in Traveling, App development. Based in Hyderabad, India. Catch me at https://about.me/satyapavankumar

Responses (2)

Write a response

What a great in depth view of writing unit tests. I’ve noticed a lot of developers know that unit tests are important, but sometimes have never learned how to write them correctly.

If you write about unit tests more, I’ve recently started a…