Photo by ThisIsEngineering from Pexels

Getting Started with TDD: Kotlin and Jetpack Compose

Paul Allies
4 min readOct 27, 2021

The TDD (Test Driven Development) approach is important when it comes to testing small portions of business logic code in isolation.

In this blog, I’ll cover how I would use TDD to build a clean architecture To-do Jetpack Compose Android application

I’ll cover implementing the “Get To-dos” use case.

For this type of application, we need 2 types for application testing; UI Testing and Unit Testing. We’ll be concentrating on the business logic, so we’ll only be covering TDD unit testing. Before we write any production code, we’ll write the test first.

The application will be structured in the following file and folder structure:

.
.
├── src
├── Data
│ ├── DataSource
│ │ ├── TodoDataSource.kt
│ │ └── DB
│ │ ├── Entity
│ │ │ └── TodoDBEntity.kt
│ │ └── TodoDBDataSourceImpl.kt
│ └── Repository
│ └── TodoRepositoryImpl.kt
├── Domain
│ ├── Model
│ │ └── Todo.kt
│ ├── Repository
│ │ └── TodoRepository.kt
│ └── UseCase
│ └── GetTodos.kt
└── Presentation
├── TodoViewModel.kt
└── TodoListView.kt
└── test
├── Data
│ ├── DataSource
│ │ └── DB
│ │ └── TodoDBDataSourceImplTest.kt
│ └── Repository
│ └── TodoRepositoryImplTest.kt
├── Domain
│ └── UseCase
│ └── GetTodosTest.kt
└── Presentation
└── TodoViewModelTest.kt

We’d like to put the “TodoListViewModel” under test.

A note on TDD

TDD (Test Driven Development) is a software development process in which the unit test will be written first and after that the original code. In TDD, the design and development of the code are through unit tests.

In the traditional unit tests, the unit test is written after the original code is written. This is only for long-term maintainability. But in TDD the unit test is written first and code evolved through it.

Red Green Refactor

Red Green Refactor is an interesting concept in TDD. The stages are given below:

Red — First a failing unit test is created, and it results in red status

Green — We will modify the associated code to just make the unit test pass — resulting in green status

Refactor — Once the test is passing we can refactor the code so that the original implementation is done.

When you create an android project, Android Studio automatically creates a UI Test (androidTest) Package and Unit Test (test) Package.

Let’s start by writing a test for our View Model.

test_TodoListViewModel_Should_Exist

Obviously, the test doesn’t build and therefore fails because these files don’t exist. We’re in Red Stage.

Let’s write the minimum code to make the test pass. We need a view model and its mock dependency.

The test will now pass — Green Stage

Let’s add a few more tests to drive the development of the view model

test_TodoListViewModel_Should_Return_An_Empty_Todos_List

test_TodoListViewModel_Should_Return_4_Todos_When_getTodos_Is_Invoked

test_TodoListViewModel_Should_Display_Error_Message_When_getTodos_Throws_Exception

We can now continue down the dependency chain, unit testing, and mocking dependencies until we complete the “get todos” use case.

Originally published at https://codefoundation.co.za.

--

--