Testing Kotlin Flows in Android using Turbine
In this article, we will explore some tips and techniques for testing Kotlin Flows in Android apps, helping you write more reliable and robust code.
Why is this important?
Testing is an essential part of software development, and it becomes even more critical when working with asynchronous code like Flows.
Setup
Go ahead to :app/build.gradle
and add Turbine
.
dependencies {
// You can also use JUnit instead of kotlin-test
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("app.cash.turbine:turbine:0.12.1")
}
What is Turbine and how it works?
Turbine
is a small testing library for Kotlin Flows. A Turbine
is a thin wrapper over a Channel
with an API designed for testing.
Functions
awaitItem()
— Suspends the function and awaits the new value to be sent to the Turbine
.
awaitComplete()
— Suspends the function until the Turbine
completes without an exception.
awaitError()
— Suspends the function until the Turbine
completes with a Throwable
.