Unit testing viewModel that uses StateFlow and Coroutines - android

Kotlin 1.4.21
I have a very simple ViewModel that uses coroutine and stateFlow. However, the unit test will fail as the stateFlow doesn't seem to get updated.
I think its because the test will finish before the stateFlow is updated.
expected not to be empty
This is my ViewModel under test
class TrendingSearchViewModel #Inject constructor(
private val loadTrendingSearchUseCase: LoadTrendingSearchUseCase,
private val coroutineDispatcher: CoroutineDispatcherProvider
) : ViewModel() {
private val trendingSearchMutableStateFlow = MutableStateFlow<List<String>>(emptyList())
val trendingSearchStateFlow = trendingSearchMutableStateFlow.asStateFlow()
fun getTrendingSearch() {
viewModelScope.launch(coroutineDispatcher.io()) {
try {
trendingSearchMutableStateFlow.value = loadTrendingSearchUseCase.execute()
} catch (exception: Exception) {
Timber.e(exception, "trending ${exception.localizedMessage}")
}
}
}
}
This is my actual test class, I have tried different things to get it to work
class TrendingSearchViewModelTest {
private val loadTrendingSearchUseCase: LoadTrendingSearchUseCase = mock()
private val coroutineDispatcherProvider = CoroutineDispatcherProviderImp()
private lateinit var trendingSearchViewModel: TrendingSearchViewModel
#Before
fun setUp() {
trendingSearchViewModel = TrendingSearchViewModel(
loadTrendingSearchUseCase,
coroutineDispatcherProvider
)
}
#Test
fun `should get trending search suggestions`() {
runBlocking {
// Arrange
val trending1 = UUID.randomUUID().toString()
val trending2 = UUID.randomUUID().toString()
val trending3 = UUID.randomUUID().toString()
whenever(loadTrendingSearchUseCase.execute()).thenReturn(listOf(trending1, trending2, trending3))
val job = launch {
trendingSearchViewModel.trendingSearchStateFlow.value
}
// Act
trendingSearchViewModel.getTrendingSearch()
// Assert
val result = trendingSearchViewModel.trendingSearchStateFlow.value
assertThat(result).isNotEmpty()
job.cancel()
}
}
}
This is the usecase I am mocking in the test:
class LoadTrendingSearchUseCaseImp #Inject constructor(
private val searchCriteriaProvider: SearchCriteriaProvider,
private val coroutineDispatcherProvider: CoroutineDispatcherProvider
) : LoadTrendingSearchUseCase {
override suspend fun execute(): List<String> {
return withContext(coroutineDispatcherProvider.io()) {
searchCriteriaProvider.provideTrendingSearch().trendingSearches
}
}
}
Just in case its needed this is my interface:
interface CoroutineDispatcherProvider {
fun io(): CoroutineDispatcher = Dispatchers.IO
fun default(): CoroutineDispatcher = Dispatchers.Default
fun main(): CoroutineDispatcher = Dispatchers.Main
fun immediate(): CoroutineDispatcher = Dispatchers.Main.immediate
fun unconfined(): CoroutineDispatcher = Dispatchers.Unconfined
}
class CoroutineDispatcherProviderImp #Inject constructor() : CoroutineDispatcherProvider

I think this library https://github.com/cashapp/turbine by Jack Wharton will be of great help in the future when you need more complex scenarios.
What I think is happening is that in fragment you are calling .collect { } and that is ensuring the flow is started. Check the Terminal operator definition: Terminal operators on flows are suspending functions that start a collection of the flow. https://kotlinlang.org/docs/flow.html#terminal-flow-operators
This is not true for sharedFlow, which might be configured to be started eagerly.
So to solve your issue, you might just call
val job = launch {
trendingSearchViewModel.trendingSearchStateFlow.collect()
}

This is what worked for me:
#Test
fun `should get trending search suggestions`() {
runBlockingTest {
// Arrange
val trending1 = UUID.randomUUID().toString()
val trending2 = UUID.randomUUID().toString()
val trending3 = UUID.randomUUID().toString()
val listOfTrending = listOf(trending1, trending2, trending3)
whenever(loadTrendingSearchUseCase.execute()).thenReturn(listOfTrending)
/* List to collect the results */
val listOfEmittedResult = mutableListOf<List<String>>()
val job = launch {
trendingSearchViewModel.trendingSearchStateFlow.toList(listOfEmittedResult)
}
// Act
trendingSearchViewModel.getTrendingSearch()
// Assert
assertThat(listOfEmittedResult).isNotEmpty()
verify(loadTrendingSearchUseCase).execute()
job.cancel()
}
}

Related

Disable/cancel StateFlow for unit test

In my ViewModel there's a StateFlow which seems to prevent my unit test from ever completing, i.e. the test "hangs". I'm fairly new to the Flow lib and not sure how to cancel/disable said StateFlow so I can run my test as normal.
I created a simplified version of the code to highlight my problem. Here's the ViewModel in question:
#ExperimentalCoroutinesApi
class MyViewModel(
private val someApiClient: SomeApiClient,
private val dispatchers: CoroutineContexts = DefaultCoroutineContexts,
private val someLogger: SomeLogger
) : ViewModel() {
private val queries = MutableSharedFlow<String>()
#FlowPreview
val suggestions: StateFlow<Result<Throwable, String>> =
queries
.sample(THROTTLE_TIME)
.distinctUntilChanged()
.mapLatest {
try {
val downloadedSuggestions = someApiClient.getSuggestions(it)
Result.Success(downloadedSuggestions)
} catch (exception: Throwable) {
Result.Error(exception)
}
}
.flowOn(dispatchers.io)
.stateIn(viewModelScope, SharingStarted.Eagerly, Result.Success(""))
fun dispatchEvent(event: MyEvent) {
when (event) {
is SearchEvent -> someLogger.logStuff()
}
}
}
And the test looks like this:
#ExperimentalCoroutinesApi
class MyViewModelTest {
#get:Rule var mockitoRule: MockitoRule = MockitoJUnit.rule()
#get:Rule val coroutinesTestRule = MainDispatcherRule()
#Mock lateinit var someApiClient: SomeApiClient
#Mock lateinit var someLogger: SomeLogger
lateinit var myViewModel: MyViewModel
#Before
fun setUp() {
myViewModel = MyViewModel(
someApiClient,
coroutinesTestRule.testDispatcher.createCoroutineContexts(),
someLogger
)
}
#Test
fun `my test`() = runTest {
// When
myViewModel.dispatchEvent(SearchEvent)
// Then
verify(someLogger).logStuff()
}
}
I tried a suggestion from the Android Developer documentation page in the test:
#Test
fun `my test`() = runTest {
// Given
val dispatcher = UnconfinedTestDispatcher(testScheduler)
val job = launch(dispatcher) { myViewModel.suggestions.collect() }
// When
myViewModel.dispatchEvent(SearchEvent)
// Then
verify(someLogger).logStuff()
job.cancel()
}
But it didn't help. I feel like I'm missing something fairly obvious but can't quite put my finger on it. Any suggestions are welcome.

Test MutableSharedFlow with runTest

I’m having some trouble to test some state changes in my viewModel using MutableSharedFlow. For example, I have this class
class SampleViewModel : ViewModel() {
private val interactions = Channel<Any>()
private val state = MutableSharedFlow<Any>(
onBufferOverflow = BufferOverflow.DROP_LATEST,
extraBufferCapacity = 1
)
init {
viewModelScope.launch {
interactions.receiveAsFlow().collect { action ->
processAction(action)
}
}
}
fun flow() = state.asSharedFlow()
fun handleActions(action: Any) {
viewModelScope.launch {
interactions.send(action)
}
}
suspend fun processAction(action: Any) {
state.emit("Show loading state")
// process something...
state.emit("Show success state")
}
}
I'm trying to test it using this method (already using version 1.6.1):
#Test
fun testHandleActions() = runTest {
val emissions = mutableListOf<Any>()
val job = launch(UnconfinedTestDispatcher(testScheduler)) {
viewModel.flow().collect {
emissions.add(it)
}
}
viewModel.handleActions("")
assertTrue(emissions[0] is String)
assertTrue(emissions[1] is String)
job.cancel()
}
If I test the viewmodel.processAction() it works like a charm. But if I try to test handleActions() I only receive the first emission and then it throws an IndexOutOfBoundsException
If I use the deprecated runBlockingTest, it works
#Test
fun testHandleActionsWithRunBlockingTest() = runBlockingTest {
val emissions = mutableListOf<Any>()
val job = launch {
viewModel.flow().toList(emissions)
}
viewModel.handleActions("")
job.cancel()
assertTrue(emissions[0] is String)
assertTrue(emissions[1] is String)
}
Did I miss something using runTest block?

How to make MockWebServer + Retrofit + Coroutines run in the same dispatcher

I'm unsuccessfully trying to use MockWebServer + Retrofit + Coroutines on my Android unit tests. During debugging, I found out that OkHttp is running on a different thread, which is why my test always fails.
This is my rule to set the test dispatcher:
class MainCoroutineRule(
private val scheduler: TestCoroutineScheduler = TestCoroutineScheduler(),
val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(scheduler)
) : TestWatcher() {
val testScope = TestScope(testDispatcher)
override fun starting(description: Description) {
super.starting(description)
Dispatchers.setMain(testDispatcher)
}
override fun finished(description: Description) {
super.finished(description)
Dispatchers.resetMain()
}
}
this is my ViewModel:
#HiltViewModel
class TestViewModel #Inject constructor(
private val repository: TestRepository,
#MainDispatcher private val dispatcher: CoroutineDispatcher
) : ViewModel() {
val hasData = MutableLiveData(false)
fun fetchSomething() {
viewModelScope.launch(dispatcher) {
when (repository.getSomething()) {
is Success -> {
hasData.value = true
}
else -> {
hasData.value = false
}
}
}
}
}
And finally the test:
class TestViewModelTest : MockServerSuite() {
#get:Rule
val taskExecutorRule = InstantTaskExecutorRule()
#get:Rule
val mainTestRule = MainCoroutineRule()
#Test
fun `my test`() = mainTestRule.testScope.runTest {
// setting up MockWebServer
val viewModel = TestViewModel(
repository = TestRepository(
api = testApi(server),
),
dispatcher = mainTestRule.testDispatcher
)
viewModel.fetchSomething()
assertThat(viewModel.hasData.value).isTrue
}
}
Since Retrofit is main-safe, I have no idea how to make it run on my testDispatcher. Am I missing something?
As Petrus mentioned, I already use getOrAwaitValue, which works for simple scenarios.
Our requests fire other requests after receiving some data in most of our use cases.
Usually, I receive intermediate values of getOrAwaitValue and not the final LiveData I was expecting.
Try to use getOrAwaitValue :)
class TestViewModelTest {
#Test
fun `my test`() = mainTestRule.testScope.runTest {
// setting up MockWebServer
val viewModel = TestViewModel(
repository = TestRepository(
api = testApi(server),
),
dispatcher = mainTestRule.testDispatcher
)
viewModel.fetchSomething()
assertThat(viewModel.hasData.getOrAwaitValue()).isTrue
}
}
#VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun <T> LiveData<T>.getOrAwaitValue(
time: Long = 2,
timeUnit: TimeUnit = TimeUnit.SECONDS,
afterObserve: () -> Unit = {}
): T {
var data: T? = null
val latch = CountDownLatch(1)
val observer = object : Observer<T> {
override fun onChanged(o: T?) {
data = o
latch.countDown()
this#getOrAwaitValue.removeObserver(this)
}
}
this.observeForever(observer)
try {
afterObserve.invoke()
// Don't wait indefinitely if the LiveData is not set.
if (!latch.await(time, timeUnit)) {
throw TimeoutException("LiveData value was never set.")
}
} finally {
this.removeObserver(observer)
}
#Suppress("UNCHECKED_CAST")
return data as T
}

How to make viewModelScope wait for a suspend function in unit test

As we know the default dispatcher for viewModelScope executes in parallel. How do I make viewModelScope wait for the suspend function in unit test . In my code i would like to get the result from repository.getData() so that my assertion passes. Right now, i don't get the result on time.
class MainViewModel(private val repository: Repository,
private val dispatcher: CoroutineDispatcherProvider) : ViewModel() {
private val viewState = MutableLiveData<Results<Data>>()
fun getViewState() : LiveData<Results<Data>> = viewState
fun getData(query: search) {
viewState.value = Loading
viewModelScope.launch(dispatcher.main()) {
val results = repository.getData(query) //need to wait for this
when(results){
is Success -> {
viewState.value = Success(results.data)
}
is Error -> viewState.value = Error(“Error”)
}
}
MainViewModelTest:
Class MainViewModelTest {
#get:Rule
val instantExecutorRule = InstantTaskExecutorRule()
#get:Rule
val coroutineTestRule = CoroutineTestRule()
#Test
fun `test get data`() = coroutineTestRule.testCoroutineDispatcher.runBlockingTest {
coEvery{repository.getData(“query”)} returns Success(Data)
var observer:Observer<Results<Data>> = mock()
viewModel.getViewState().observeForever(observer)
viewModel.getData(“query”)
assertEquals(Loading, state)
assertEquals(resultSuccess, state)
}
}
I think you should use observer.onChanged() for assertions and verifications. I don't see a problem otherwise.
val successResult = Result.Success(Data)
coEvery{repository.getData(“query”)} returns successResult
verify { observer.onChanged(Result.Loading) }
verify { observer.onChanged(successResult) }

Testing Coroutines and LiveData: Coroutine result not reflected on LiveData

I am new to testing and I wanted to learn how to test Coroutines with MVVM pattern. I just followed https://github.com/android/architecture-samples project and did a few changes (removed remote source). But when testing the ViewModel for fetching data from a repository, it keeps on failing with this error.
value of : iterable.size()
expected : 3
but was : 0
iterable was: []
Expected :3
Actual :0
Below is my test class for the ViewModel don't know what I'm missing. Also when mocking the repository I can get the expected results from it when printing taskRepository.getTasks() it just doesn't reflect on the LiveData when calling loadTasks()
ViewModelTest
#ExperimentalCoroutinesApi
#RunWith(MockitoJUnitRunner::class)
class TasksViewModelTest {
private lateinit var tasksViewModel: TasksViewModel
val tasksRepository = mock(TasksRepository::class.java)
#ExperimentalCoroutinesApi
#get:Rule
var mainCoroutineRule = TestMainCoroutineRule()
// Executes each task synchronously using Architecture Components.
#get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
#Before
fun setupViewModel() {
tasksViewModel = TasksViewModel(tasksRepository)
}
#Test
fun whenLoading_hasListOfTasks() = runBlockingTest {
val task1 = Task("title1", "description1")
val task2 = Task("title2", "description2")
val task3 = Task("title3", "description3")
`when`(tasksRepository.getTasks()).thenReturn(Result.Success(listOf(
task1,
task2,
task3
)))
tasksViewModel.loadTasks()
val tasks = LiveDataTestUtil.getValue(tasksViewModel.tasks)
assertThat(tasks).hasSize(3)
}
}
TasksViewModel
class TasksViewModel #Inject constructor(
private val repository: TasksRepository
) : ViewModel() {
private val _tasks = MutableLiveData<List<Task>>().apply { value = emptyList() }
val tasks: LiveData<List<Task>> = _tasks
fun loadTasks() {
viewModelScope.launch {
val tasksResult = repository.getTasks()
if (tasksResult is Success) {
val tasks = tasksResult.data
_tasks.value = ArrayList(tasks)
}
}
}
}
Helper classes are listed below, I just copied the same classes from the sample project.
LiveDataTestUtil
object LiveDataTestUtil {
/**
* Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds.
* Once we got a notification via onChanged, we stop observing.
*/
fun <T> getValue(liveData: LiveData<T>): T {
val data = arrayOfNulls<Any>(1)
val latch = CountDownLatch(1)
val observer = object : Observer<T> {
override fun onChanged(o: T?) {
data[0] = o
latch.countDown()
liveData.removeObserver(this)
}
}
liveData.observeForever(observer)
latch.await(2, TimeUnit.SECONDS)
#Suppress("UNCHECKED_CAST")
return data[0] as T
}
}
MainCoroutineRule
#ExperimentalCoroutinesApi
class TestMainCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
}
}
Turns out it was a problem with mockito, I have an older version, and I found there's a library called mockito-kotlin to simplify testing coroutines as stated here. I then chaged my code to this and It's working well.
tasksRepository.stub {
onBlocking { getTasks() }.doReturn(Result.Success(listOf(task1, task2, task3)))
}

Categories

Resources