Exercise: Flow testing
Test the observeAppointments
function from the ObserveAppointmentsUseCase
class. Starting code:
class ObserveAppointmentsUseCase(
private val appointmentRepository: AppointmentRepository
) {
fun observeAppointments(): Flow<List<Appointment>> =
appointmentRepository
.observeAppointments()
.filterIsInstance<AppointmentUpdate>()
.map { it.appointments }
.distinctUntilChanged()
.retry { it is ApiException && it.code in 500..599 }
}
interface AppointmentRepository {
fun observeAppointments(): Flow<AppointmentEvent>
}
sealed class AppointmentEvent
data class AppointmentUpdate(
val appointments: List<Appointment>
) : AppointmentEvent()
data object AppointmentConfirmed : AppointmentEvent()
data class Appointment(val title: String, val time: Instant)
data class ApiException(val code: Int) : Throwable()
You should test the following cases:
- should receive only appointment lists from appointment updates
- should not receive non-distinct values
- should retry exceptions of type
ApiException
with code 5XX
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in coroutines/flow/ObserveAppointmentsUseCase.kt. You can find there starting code.
import kotlinx.coroutines.flow.*
import java.time.Instant
class ObserveAppointmentsUseCase(
private val appointmentRepository: AppointmentRepository
) {
fun observeAppointments(): Flow<List<Appointment>> =
appointmentRepository
.observeAppointments()
.filterIsInstance<AppointmentUpdate>()
.map { it.appointments }
.distinctUntilChanged()
.retry { it is ApiException && it.code in 500..599 }
}
interface AppointmentRepository {
fun observeAppointments(): Flow<AppointmentEvent>
}
sealed class AppointmentEvent
data class AppointmentUpdate(
val appointments: List<Appointment>
) : AppointmentEvent()
data object AppointmentConfirmed : AppointmentEvent()
data class Appointment(val title: String, val time: Instant)
data class ApiException(val code: Int) : Throwable()
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.