Share
Embed
Unit Testing Channels & Flows
Unit testing Channels and Flows can be a challenge as they are fairly new. In this talk, I will share with you how to implement and test practical examples from my experience. These examples are testing delays, retries, and errors. I'll also share testing more complex examples such as polling. For each use case, we'll look at how to use features in the coroutines library such as runBlockingTest and TestCoroutineDispatcher. From my journey of using and testing Flows in production, I'll share the challenges I experienced.
Mohit S
June 03, 2020
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
145 slides
-
Unit Test Kotlin Channels & Flows • Use case with
ViewModel & Repo • Testing Repo & ViewModel • Flow Assertions • Test Pattern used in Coroutine Library -
Repository class UserRepository(val apiService: ApiService) { fun userDetails(id: Int): Flow<Result<UserDetails">>
{ } } How do we create this Flow? -
fun userDetails(id: Int): Flow<Result<UserDetails">> { } fun <T> flow( block:
suspend FlowCollector<T>.() "-> Unit ): Flow<T> Creating Flow Coroutine Library -
fun userDetails(id: Int): Flow<Result<UserDetails">> { } interface FlowCollector<in T> {
suspend fun emit(value: T) } Coroutine Library Creating Flow -
Creating Flow fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow {
val userDetails = apiService.userDetails(id) emit(Result.success(userDetails)) } } -
Creating Flow fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow {
val userDetails = apiService.userDetails(id) emit(Result.success(userDetails)) } } separate thread? -
Creating Flow fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow {
val userDetails = apiService.userDetails(id) emit(Result.success(userDetails)) } } Coroutine Library fun Flow<T>.flowOn(context: CoroutineContext): Flow<T> -
Creating Flow fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow {
val userDetails = apiService.userDetails(id) emit(Result.success(userDetails)) } } Coroutine Library Dispatcher • Default, IO, Main, etc""... -
Creating Flow class UserRepository( val apiService: ApiService, val dispatcher: CoroutineDispatcher
) { fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val users = apiService.userDetails(id) emit(Result.success(users)) }.flowOn(dispatcher) } -
Creating Flow class UserRepository( val apiService: ApiService, val dispatcher: CoroutineDispatcher
) { fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val users = apiService.userDetails(id) emit(Result.success(users)) }.flowOn(dispatcher) } -
View Model class UserDetailViewModel : ViewModel { val scope =
CoroutineScope(Dispatchers.Main) } Coroutine Library fun CoroutineScope(context: CoroutineContext) -
View Model class UserDetailViewModel : ViewModel { val scope =
CoroutineScope(Dispatchers.Main) scope.launch { } } -
View Model class UserDetailViewModel( val repository: UserRepository ) : ViewModel
{ val scope = CoroutineScope(Dispatchers.Main) scope.launch { } } -
View Model class UserDetailViewModel( val repository: UserRepository ) : ViewModel
{ val scope = CoroutineScope(Dispatchers.Main) scope.launch { val flow = repository.getUserDetails(id = 1) } } -
View Model class UserDetailViewModel( val repository: UserRepository ) : ViewModel
{ val scope = CoroutineScope(Dispatchers.Main) scope.launch { val flow = repository.getUserDetails(id = 1) } } Coroutine Library fun <T> Flow<T>.collect( action: suspend (value: T) "-> Unit ) -
View Model class UserDetailViewModel( val repository: UserRepository ) : ViewModel
{ val scope = CoroutineScope(Dispatchers.Main) scope.launch { val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> } } -
View Model class UserDetailViewModel( val repository: UserRepository val reducer: Reducer
) : ViewModel { val scope = CoroutineScope(Dispatchers.Main) scope.launch { val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> reducer.dispatchState(result) } } -
View Model class UserDetailViewModel( val repository: UserRepository, val reducer: Reducer
) : ViewModel { val scope = CoroutineScope(Dispatchers.Main) scope.launch { val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> reducer.dispatchState(result) } } -
val ViewModel.viewModelScope: CoroutineScope get() { val scope: CoroutineScope? = this.getTag(JOB_KEY)
return scope } View Model class UserDetailViewModel( val repository: UserRepository, val stateManager: StateManager ) : ViewModel { val scope = CoroutineScope(Dispatchers.Main) Architecture Components -
Kotlin Coroutines Test Library • Test Coroutine Scope • Test
Coroutine Dispatcher • runBlockingTest testImplementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-test:x.x.x' -
Unit Test Repository Cases • Flow emits Success • Flow
emits Error • Retries with delay (Advanced) -
Repository class UserRepository( val apiService: ApiService, val dispatcher: CoroutineDispatcher )
{ fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val users = apiService.userDetails(id) emit(Result.success(users)) }.flowOn(dispatcher) } -
@Test fun `should get users details on success`() = runBlocking
{ } Coroutine Library RunBlocking • Creates a coroutine • Blocks until coroutine completes -
@Test fun `should get users details on success`() = runBlocking
{ val userDetails = UserDetails(1, "User 1", "avatar_url") val apiService = mock<ApiService>() } suspend fun userDetails(id: Int): UserDetails API Service -
@Test fun `should get users details on success`() = runBlocking
{ val userDetails = UserDetails(1, "User 1", "avatar_url") val apiService = mock<ApiService>() { on { userDetails(1) } doReturn userDetails } Error: Suspend functions can only be called from suspend functions -
@Test fun `should get users details on success`() = runBlocking
{ val userDetails = UserDetails(1, "User 1", "avatar_url") val apiService = mock<ApiService>() { on { userDetails(1) } doReturn userDetails } } Mockito Kotlin fun <R> on(methodCall: T.() "-> R) -
Mockito Kotlin fun onBlocking(m: suspend T.() "-> R) { return
runBlocking { Mockito.`when`(mock.m()) } } @Test fun `should get users details on success`() = runBlocking { val userDetails = UserDetails(1, "User 1", "avatar_url") val apiService = mock<ApiService>() { on { userDetails(1) } doReturn userDetails } } -
@Test fun `should get users details on success`() = runBlocking
{ val userDetails = UserDetails(1, "User 1", "avatar_url") val apiService = mock<ApiService>() { onBlocking { userDetails(1) } doReturn userDetails } } -
@Test fun `should get users details on success`() = runBlocking
{ val userDetails = mockUserDetails() } -
@Test fun `should get users details on success`() = runBlocking
{ val userDetails = mockUserDetails() val dispatcher = TestCoroutineDispatcher() val repository = UserRepository(userService, dispatcher) val flow = repository.getUserDetails(id = 1) } -
@Test fun `should get users details on success`() = runBlocking
{ val userDetails = mockUserDetails() val dispatcher = TestCoroutineDispatcher() val repository = UserRepository(userService, dispatcher) val flow = repository.getUserDetails(id = 1) val result = flow.single() result.isSuccess.assertTrue() } -
Unit Test Repository Cases • Flow emits Success • Flow
emits Error • Retries with delay (Advanced) -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.flowOn(dispatcher) } Exception -
View Model scope.launch { val flow: = userRepository.getUserDetails(id = 1)
flow.collect { } } fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails = apiService.userDetails(id) emit(Result.success(userDetails)) }.flowOn(dispatcher) } Exception -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.flowOn(dispatcher) } Coroutine Library fun <T> Flow<T>.catch( action: suspend FlowCollector<T>.(t: Throwable) "-> Unit ): Flow<T> -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Exception -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Exception -
@Test fun `should get error for user details`() = runBlocking
{ val apiService = mock<ApiService> { onBlocking { userDetails(1) } doAnswer { throw IOException() } } } Mock -
@Test fun `should get error for user details`() = runBlocking
{ val apiService = mockApiService() val repository = UserRepository(apiService, dispatcher) val flow = repository.getUserDetails(id = 1) } -
@Test fun `should get error for user details`() = runBlocking
{ val apiService = mockApiService() val repository = UserRepository(apiService, dispatcher) val flow = repository.getUserDetails(id = 1) val result = flow.single() result.isFailure.assertTrue() } -
Unit Test Repository Cases • Flow emits Success • Flow
emits Error • Retries with delay (Advanced) -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Exception -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Coroutine Library fun retry( retries: Long, block: suspend (Throwable) "-> Boolean ): Flow<T> -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.catch { emit(Result.failure(it)) } .retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Exception -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.catch { emit(Result.failure(it)) } .retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Retry -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.catch { emit(Result.failure(it)) } .retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Delay 1s -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.catch { emit(Result.failure(it)) } .retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Retry -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } -
Unit Test Retry Cases • All Retries fail with Error
• Retry succeeds -
runBlockingTest 1. Launch a coroutine with a test scope and
test dispatcher. 2. Advance virtual time forward. -
@Test fun `should retry with error`() { } Coroutine Library
fun runBlockingTest( block: suspend TestCoroutineScope.() "-> Unit ) fun TestCoroutineDispatcher.runBlockingTest( block: suspend TestCoroutineScope.() "-> Unit ) -
val dispatcher = TestCoroutineDispatcher() @Test fun `should retry with error`()
= dispatcher.runBlockingTest { } Create coroutines with test dispatcher -
@Test fun `should retry with error`() = dispatcher.runBlockingTest { val
apiService = mock<ApiService> { onBlocking { userDetails(1) } doAnswer { throw IOException() } } } -
@Test fun `should retry with error`() = dispatcher.runBlockingTest { val
apiService = mockApiService() val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> result.isFailure.assertTrue() } } -
@Test fun `should retry with error`() = dispatcher.runBlockingTest { val
apiService = mockApiService() val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> result.isFailure.assertTrue() } } runBlockingTest -
@Test fun `should retry with error`() = dispatcher.runBlockingTest { val
apiService = mockApiService() val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> result.isFailure.assertTrue() } } runBlockingTest -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } runBlockingTest -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } runBlockingTest -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val users
= userService.userDetails(id) emit(Result.success(users)) }.retry(retries = 2) { t "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } runBlockingTest Coroutines Test Library fun runBlockingTest { dispatcher.advanceUntilIdle() } -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) runBlockingTest -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Catch Error. All retries failed. -
@Test fun `should retry with error`() = dispatcher.runBlockingTest { val
apiService = mockApiService() val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> result.isFailure.assertTrue() } } -
Unit Test Retry Cases • All Retries fail with Error
• Retry succeeds -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Retry -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Success -
runBlockingTest Features • Pause Dispatcher • Advance virtual time forward
by certain milliseconds. -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { var
throwError = true } Control API Response -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { var
throwError = true val userDetails = UserDetails(1, "User 1", "avatar_url") } Successful Response -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { var
throwError = true val userDetails = UserDetails(1, "User 1", "avatar_url") val apiService = mock<ApiService> { } } -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { var
throwError = true val userDetails = UserDetails(1, "User 1", "avatar_url") val apiService = mock<ApiService> { onBlocking { userDetails(1) } doAnswer { if (throwError) throw IOException() else userDetails } } } Control API Response -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { val
apiService = mockApiService() pauseDispatcher { } } Coroutine Test Library pausedDispatcher • Do not start coroutines eagerly -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { val
apiService = mockApiService() pauseDispatcher { val flow = repository.getUserDetails(id = 1) launch { flow.collect { it.isSuccess.assertTrue() } } } Consumer -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Run Flow -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Suspend -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { val
apiService = mockApiService() pauseDispatcher { val flow = repository.getUserDetails(id = 1) launch { flow.collect { it.isSuccess.assertTrue() } } advanceTimeBy(DELAY_ONE_SECOND) } } Advance virtual time -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Retry -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Suspend -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { val
apiService = mockApiService() pauseDispatcher { val flow = repository.getUserDetails(id = 1) launch { flow.collect { it.isSuccess.assertTrue() } } advanceTimeBy(DELAY_ONE_SECOND) throwError = false } } Return success -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { val
apiService = mockApiService() pauseDispatcher { val flow = repository.getUserDetails(id = 1) launch { flow.collect { it.isSuccess.assertTrue() } } advanceTimeBy(DELAY_ONE_SECOND) throwError = false advanceTimeBy(DELAY_ONE_SECOND) } } Advance virtual time -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Retry -
fun userDetails(id: Int): Flow<Result<UserDetails">> { return flow { val userDetails
= apiService.userDetails(id) emit(Result.success(userDetails)) }.retry(retries = 2) { t: Throwable "-> (t is Exception).also { if (it) delay(DELAY_ONE_SECOND) } } .catch { emit(Result.failure(it)) } .flowOn(dispatcher) } Emit Success -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { val
apiService = mockApiService() pauseDispatcher { val flow = repository.getUserDetails(id = 1) launch { flow.collect { it.isSuccess.assertTrue() } } advanceTimeBy(DELAY_ONE_SECOND) throwError = false advanceTimeBy(DELAY_ONE_SECOND) } } Collect -
@Test fun `should retry with success`() = dispatcher.runBlockingTest { val
apiService = mockApiService() pauseDispatcher { val flow = repository.getUserDetails(id = 1) launch { flow.collect { it.isSuccess.assertTrue() } } advanceTimeBy(DELAY_ONE_SECOND) throwError = false advanceTimeBy(DELAY_ONE_SECOND) } } -
Unit Test Repository Cases • Flow emits Success • Flow
emits Error • Retries with delay -
View Model class UserDetailViewModel( val repository: UserRepository, val stateManager: StateManager
) : ViewModel { viewModelScope.launch { val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> stateManager.dispatchState(result) } } } -
View Model class UserDetailViewModel( val repository: UserRepository, val stateManager: StateManager
) : ViewModel { viewModelScope.launch { val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> stateManager.dispatchState(result) } } } Main Dispatcher -
Test Rule class CoroutineTestRule( val dispatcher= TestCoroutineDispatcher() ) : TestWatcher()
{ override fun starting(description: Description?) { super.starting(description) Dispatchers.setMain(dispatcher) } } -
Test Rule class CoroutineTestRule( val dispatcher= TestCoroutineDispatcher() ) : TestWatcher()
{ override fun finished(description: Description?) { super.finished(description) Dispatchers.resetMain() dispatcher.cleanupTestCoroutines() } } -
View Model Test @get:Rule val rule = CoroutineTestRule() val repository
= mock<UserRepository>() val stateManager = mock<StateManager>() val viewModel = UserDetailsViewModel(repository, stateManager) -
@Test fun `should dispatch details`() = rule.dispatcher.runBlockingTest { } Create
coroutine with TestDispatcher -
class UserDetailViewModel( val repository: UserRepository, val stateManager: StateManager ) :
ViewModel { viewModelScope.launch { val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> stateManager.dispatchState(result) } } } Mock Repo -
class UserDetailViewModel( val repository: UserRepository, val stateManager: StateManager ) :
ViewModel { viewModelScope.launch { val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> stateManager.dispatchState(result) } } } Trigger Flow Collection -
@Test fun `should dispatch details`() = rule.dispatcher.runBlockingTest { val userDetails
= UserDetails(1, "User 1", "avatar") val result = Result.success(userDetails) } -
@Test fun `should dispatch details`() = rule.dispatcher.runBlockingTest { val userDetails
= UserDetails(1, "User 1", "avatar") val result = Result.success(userDetails) val channel = Channel<Result<UserDetails">>() } Channel -
@Test fun `should dispatch details`() = rule.dispatcher.runBlockingTest { val userDetails
= UserDetails(1, "User 1", "avatar") val result = Result.success(userDetails) val channel = Channel<Result<UserDetails">>() val flow = channel.consumeAsFlow() } Convert to Flow Flow Channel Convert to Flow -
@Test fun `should dispatch details`() = rule.dispatcher.runBlockingTest { val userDetails
= UserDetails(1, "User 1", "avatar") val result = Result.success(userDetails) val channel = Channel<Result<UserDetails">>() val flow = channel.consumeAsFlow() whenever(repository.getUserDetails(id = 1)) doReturn flow } Return Flow from Channel -
@Test fun `should dispatch details`() = rule.dispatcher.runBlockingTest { val result
= mockUserDetailsResult() val channel = Channel<Result<UserDetails">>() val flow = channel.consumeAsFlow() whenever(repository.getUserDetails(id = 1)) doReturn flow launch { channel.send(result) } } Producer to send values -
@Test fun `should dispatch details`() = rule.dispatcher.runBlockingTest { val result
= mockUserDetailsResult() val channel = Channel<Result<UserDetails">>() val flow = channel.consumeAsFlow() whenever(repository.getUserDetails(id = 1)) doReturn flow launch { channel.send(result) } userDetailsViewModel.getUserDetails() } Run test method -
class UserDetailViewModel( val repository: UserRepository, val stateManager: StateManager ) :
ViewModel { viewModelScope.launch { val flow = repository.getUserDetails(id = 1) flow.collect { result: Result<UserDetails> "-> stateManager.dispatchState(result) } } } Collect result from Flow -
@Test fun `should dispatch details`() = rule.dispatcher.runBlockingTest { val result
= mockUserDetailsResult() val channel = Channel<Result<UserDetails">>() val flow = channel.consumeAsFlow() whenever(repository.getUserDetails(id = 1)) doReturn flow launch { channel.send(result) } userDetailsViewModel.getUserDetails() verify(stateManager).dispatch(result) } Verify result -
Flow Assertions @Test fun `should get users details on success`()
= runBlocking { ""... flow.collect { } flow.single() } RxJava Observable.test() -
Flow Assertions API fun expectItem(): T fun expectNoMoreEvents() fun expectComplete()
fun expectError(): Throwable Test Flow Channel -
sealed class Event<out T> { object Complete : Event<Nothing>() data
class Error(val t: Throwable) : Event<Nothing>() data class Item<T>(val item: T) : Event<T>() } Flow Assertions Test Flow Channel -
Flow Assertions sealed class Event<out T> { object Complete :
Event<Nothing>() data class Error(val t: Throwable) : Event<Nothing>() data class Item<T>(val item: T) : Event<T>() } Item Test Flow Channel -
Flow Assertions sealed class Event<out T> { object Complete :
Event<Nothing>() data class Error(val t: Throwable) : Event<Nothing>() data class Item<T>(val item: T) : Event<T>() } Test Flow Channel Flow emission complete -
sealed class Event<out T> { object Complete : Event<Nothing>() data
class Error(val t: Throwable) : Event<Nothing>() data class Item<T>(val item: T) : Event<T>() } Flow Assertions Test Flow Channel Error -
Flow Assertions suspend fun <T> Flow<T>.test( validate: suspend FlowAssert<T>.() "->
Unit ) { coroutineScope { val events = Channel<Event<T">>(UNLIMITED) } } Unlimited Buffered Channel -
Flow Assertions suspend fun <T> Flow<T>.test( validate: suspend FlowAssert<T>.() "->
Unit ) { coroutineScope { val events = Channel<Event<T">>(UNLIMITED) launch { collect { item "-> events.send(Event.Item(item)) } Event.Complete } } } Send to Channel -
Flow Assertions suspend fun <T> Flow<T>.test( validate: suspend FlowAssert<T>.() "->
Unit ) { coroutineScope { val events = Channel<Event<T">>(UNLIMITED) launch { try { sendToChannel() } catch (t: Throwable) { send(Event.Error(t)) } } } } Send Error -
Flow Assertions suspend fun <T> Flow<T>.test( validate: suspend FlowAssert<T>.() "->
Unit ) { coroutineScope { val events = Channel<Event<T">>(UNLIMITED) launch { collect { item "-> events.send(Event.Item(item)) } Event.Complete } } } -
Flow Assertions class FlowAssert<T>(val events: Channel<Event<T">>) { suspend fun expectItem():
T suspend fun expectComplete() suspend fun expectError(): Throwable … } -
Flow Assertions @Test fun `should get users details on success`()
= runBlocking { flow.test { expectItem() assertEquals userDetails expectComplete() } } -
Kotlin Coroutines Library Testing class CoroutinesTest : TestBase() { @Test
fun testSimple() = runTest { expect(1) finish(2) } } Expect -
Kotlin Coroutines Library Testing class CoroutinesTest : TestBase() { @Test
fun testSimple() = runTest { expect(1) finish(2) } } Finish -
Kotlin Coroutines Library Testing class CoroutinesTest : TestBase() { @Test
fun testSimple() = runTest { expect(1) finish(2) } } Test Base -
Kotlin Coroutines Library Testing expect open class TestBase { fun
error(message: Any, cause: Throwable? = null): Nothing fun expect(index: Int) fun finish(index: Int) fun runTest( expected: ((Throwable) "-> Boolean)? = null, unhandled: List<(Throwable) "-> Boolean> = emptyList(), block: suspend CoroutineScope.() "-> Unit ) -
Kotlin Coroutines Library Testing JVM JS Native Common TestBase.kt TestBase.kt
TestBase.kt TestBase.kt -
Kotlin Coroutines Library Testing actual open class TestBase actual constructor()
{ actual fun runTest( expected: ((Throwable) "-> Boolean)? = null, unhandled: List<(Throwable) "-> Boolean> = emptyList(), block: suspend CoroutineScope.() "-> Unit ) { runBlocking( block = block, context = CoroutineExceptionHandler { }) } -
Kotlin Coroutines Library Testing actual open class TestBase actual constructor()
{ actual fun runTest( expected: ((Throwable) "-> Boolean)? = null, unhandled: List<(Throwable) "-> Boolean> = emptyList(), block: suspend CoroutineScope.() "-> Unit ) { runBlocking( block = block, context = CoroutineExceptionHandler { }) } -
Kotlin Coroutines Library Testing class CoroutinesTest : TestBase() { @Test
fun testSimple() = runTest { expect(1) finish(2) } } Expect -
Kotlin Coroutines Library Testing private var actionIndex = AtomicInteger() actual
fun expect(index: Int) { val wasIndex = actionIndex.incrementAndGet() if (VERBOSE) println("expect($index), wasIndex=$wasIndex") check(index "== wasIndex) { “Expecting action index $index but it is actually $wasIndex" } } -
Kotlin Coroutines Library Testing class CoroutinesTest : TestBase() { @Test
fun testSimple() = runTest { expect(1) finish(2) } } Finish -
Kotlin Coroutines Library Testing private var finished = AtomicBoolean() actual
fun finish(index: Int) { expect(index) check(!finished.getAndSet(true)) { "Should call 'finish(""...)' at most once" } } -
Kotlin Coroutines Library Testing class CoroutinesTest : TestBase() { @Test
fun testSimple() = runTest { expect(1) finish(2) } } -
Resources • Unit Testing Delays, Errors & Retries with Kotlin
Flows https:"//codingwithmohit.com/coroutines/unit-testing-delays- errors-retries-with-kotlin-flows/ • Kotlin Assert Flow Delight https:"//codingwithmohit.com/coroutines/kotlin-assert-flow-delight/ • Channels & Flows in Practice https:"//speakerdeck.com/heyitsmohit/channels-and-flows-in-practice