Paul Allies
Code Foundation, a division of Nanosoft

Code Foundation, a division of Nanosoft

Getting Started with TDD: Kotlin and Jetpack Compose

Getting Started with TDD: Kotlin and Jetpack Compose

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 "TodoViewModel" 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

Screenshot 2021-10-27 at 11.09.59.png

Obviously the test doesn't build and therefore fails because these file 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.

class TodoViewModel constructor(private val GetTodos: GetTodos) : ViewModel() {

}
interface GetTodos {
    suspend operator fun invoke(): List<Todo>
}
class MockGetTodosUseCase : GetTodos {

    override suspend operator fun invoke(): List<Todo> {
        var list = listOf(
            Todo(id = 1, title = "One", isComplete = true),
            Todo(id = 2, title = "Two", isComplete = false),
            Todo(id = 3, title = "Three", isComplete = true),
            Todo(id = 4, title = "Four", isComplete = false),
        )

        return list
    }
}

The test will now pass - Green Stage

Screenshot 2021-10-27 at 11.11.40.png

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


test_TodoListViewModel_Should_Return_An_Empty_Todos_List

Screenshot 2021-10-27 at 11.13.26.png
class TodoViewModel constructor(private val GetTodos: GetTodos) : ViewModel() {
    private val _todos = mutableStateListOf<Todo>()
    val todos: List<Todo>
        get() = _todos
}
Screenshot 2021-10-27 at 11.15.17.png

test_TodoListViewModel_Should_Return_4_Todos_When_getTodos_Is_Invoked

Screenshot 2021-10-27 at 11.18.46.png
class TodoViewModel constructor(private val GetTodos: GetTodos) : ViewModel() {
    private val _todos = mutableStateListOf<Todo>()
    val todos: List<Todo>
        get() = _todos

    suspend fun getTodos() {
        _todos.clear()
        val result = GetTodos()
        _todos.addAll(result)
    }

}
Screenshot 2021-10-27 at 11.20.08.png

test_TodoListViewModel_Should_Display_Error_Message_When_getTodos_Throws_Exception

Screenshot 2021-10-27 at 11.22.30.png
class MockGetTodosThrowingUseCase : GetTodos {

    override suspend operator fun invoke(): List<Todo> {
        throw Exception("Use Case Error")
    }
}

class TodoViewModel constructor(private val GetTodos: GetTodos) : ViewModel() {
    private val _todos = mutableStateListOf<Todo>()
    val todos: List<Todo>
        get() = _todos

    private val _errorMessage = mutableStateOf("")
    val errorMessage: String
        get() = _errorMessage.value

    suspend fun getTodos() {
        try {
            _todos.clear()
            val result = GetTodos()
            _todos.addAll(result)
        } catch (err: Exception) {
            _errorMessage.value = err.message.toString()
        }
    }
}
Screenshot 2021-10-27 at 11.23.21.png

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

 
Share this