Coroutines interface StackOverflowService { @GET("/users") suspend fun getTopUsers(): List @GET("/users/{userId}/badges") suspend fun getBadges(@Path("userId") userId: Int): List }
Repository fun getMovies(): Flow> { return flow { // exectute API call and map to UI object val moviesList = api.getPopularMovies() .map { //do something } // Emit the list to the stream emit(moviesList) }.flowOn(Dispatchers.IO) }
Testing Kotlin Flow • Use MockK for mocking data • Use runBlockingTest scope & TestCoroutineDispatcher for testing suspend functions • Use turbine library for better assertions on flow results
Writing better assertions with Turbine Turbine gives you an API to collect items, errors and verify that nothing else has been emitted from your Flow https://github.com/cashapp/turbine
Step 2: Write network call in Repository suspend fun getComment(id: Int) = flow { // get the comment Data from the api val comment=apiService.getComments(id) // Emit this data wrapped in // the helper class [CommentApiState] emit(CommentApiState.success(comment)) }.flowOn(dispatcher)