Unit Testing with KotlinFlow
summary
Practical recipes for unit-testing Kotlin Flow: use kotlinx-coroutines-test with runTest (virtual time) to control delays, combine Kluent assertions and Turbine’s test { } API to assert first/last/in-order/no-exception for a three-emission flow, and inject a test dispatcher into StateFlow so hot-vs-cold behavior is reproducible. Demonstrates that StateFlow only exposes the latest value unless collected eagerly and that aligning dispatchers makes tests pass.
AI-written summary · about
chapters (15)
- 0:00Introduction
- 1:07Kotlin Flow recap
- 2:54Testing tools
- 4:15Cold Flow with Flow API
- 5:41Cold Flow with Turbine
- 7:06Handling delays in tests
- 7:52Switching context with coroutines
- 11:03Hot Flows intro
- 12:26StateFlow testing issues
- 14:56Solving StateFlow issues
- 16:23Turbine and concurrent emissions
- 17:25Best practices
- 18:10Flow API vs Turbine
- 20:17Turbine limits for Hot Flows
- 21:38Conclusion & Q&A
related talks
38mUnit Testing Kotlin Flows
38mUnit Testing Kotlin Channels & Flows
42mUnit Testing Kotlin Channels & Flows
43mEffective Unit testing with Kotlin
01h 29mIntro to unit testing coroutines with Kotest & MockK
44mUntangling Coroutine Testing
47mFew Tools: How To Write And Understand! Coroutines Tests
36mBest Practices for Unit Testing in Kotlin
