Unit Testing Kotlin Channels & Flows
summary
Practical guide to testing Kotlin coroutines with focused examples: uses runBlockingTest and a TestCoroutineDispatcher to control virtual time, shows advancing time (advanceUntilIdle / advanceTimeBy) to exercise delays and retries, and injects a test dispatcher into viewmodel scope via setMain and a JUnit rule. Recommends Square’s Turbine for fluent assertions (expectItem, expectError, expectComplete) and explains pitfalls: rendezvous channels and sharedFlow never completing can leave jobs active unless tests advance the correct dispatcher and consume buffered events.
AI-written summary · about
chapters (13)
- 0:00Start
- 2:33Run Blocking Test
- 8:06Types of Channels
- 14:35Flow Collector
- 14:56Result Type
- 16:20Delays
- 16:43Example of a Delay in a Curve in a Suspending Method
- 17:16Virtual Time
- 21:58Use Case for Having Delays inside of a Flow
- 26:23Jetpackviewmodel
- 27:26Shared Flow
- 35:44Resources
- 38:51Do You Have any Preferred Method or Is It Different To Test Combined Flows








