Testing in Android Part 4: Mock API Calls Or Not?

Using OkHttp Mock Web Server and Compose waitUntil

Siva Ganesh Kantamani
6 min readApr 11, 2023

--

Photo by UX Indonesia on Unsplash

Introduction

This series of testing consists of three parts, but I’m going to add a couple more parts to cover a common use case that makes this series a complete guide for Android developers.

Until now we’ve covered testing repositories, UI, and testing data sources like a local database(Room Library). Most of the apps rely on APIs, Local databases, or both, so in parts three and four, we’re going to cover how to write UI tests that are linked to asynchronous background work.

This part of the series is going to focus on understanding the problem with background tasks related to UI tests, and learning how to solve them. The next, and final part is focused on enhancing the solution with the idle resource concept(if you’re new to this, don’t worry, we’re going to cover everything from scratch).

I highly recommend reading the prequel parts in this series if you’re new to testing before going further:

Prerequisites

Concepts to be Known

We’ll be using the following components for mentioned purpose, so please make to learn them before going any further.

  1. Compose: To build the UI.
  2. Hilt: For dependency injection framework.
  3. OkHttp with Retrofit: To make API calls.
  4. Clean Architecture

A project built in Clean Architecture

We need an application built in clean architecture. You can clone my open-source project hosted on GitHub to write test cases as you learn. Clone the project from here. Make sure to use feature/basic_application_to_use branch to write test cases as you learn.

Test Dependencies

--

--