Unit test multiple LiveData values in a ViewModel init block - android

Consider the following code:
sealed interface State {
object Loading : State
data class Content(val someString: String) : State
}
class SomeViewModel(getSomeStringUseCase: GetSomeStringUseCase) : ViewModel() {
private val _someString = MutableLiveData<State>()
val someString: LiveData<State> = _someString
init {
_someString.value = State.Loading
viewModelScope.launch {
_someString.value = State.Content(getSomeStringUseCase())
}
}
}
In a unit test it's pretty simple to test and assert the last value emitted by someString, however, if I want to assert all values emitted it gets more complicated because I can't subscribe to someString before SomeViewModel is initialized and if I do the subscription right after the initialization it is too late and the values were already emitted:
class SomeViewModelTest {
#MockK
private lateinit var getSomeStringUseCase: GetSomeStringUseCase
private lateinit var viewModel: SomeViewModel
#Before
fun setUp() {
coEvery { getSomeStringUseCase() } returns "Some String!"
viewModel = SomeViewModel(getSomeStringUseCase)
}
// This test fails
#Test
fun test() {
val observer = mockk<Observer<State>>(relaxed = true)
viewModel.someString.observeForever(observer)
verifyOrder {
observer.onChanged(State.Loading)
observer.onChanged(State.Content("Some String!"))
}
}
}

Related

ViewModel unit test expected success but actual is null

I want to write a simple test for my viewModel to check if it gets data from repository. The app itself working without problem but in test, i have the following test failed.
It looks like the viewModel init block not running, because it suppose to call getUpcomingMovies() method in init blocks and post value to upcomingMovies live data object. When i test it gets null value.
Looks like i am missing a minor thing, need help to solve this.
Here is the test:
#ExperimentalCoroutinesApi
class MoviesViewModelShould: BaseUnitTest() {
private val repository: MoviesRepository = mock()
private val upcomingMovies = mock<Response<UpcomingResponse>>()
private val upcomingMoviesExpected = Result.success(upcomingMovies)
#Test
fun emitsUpcomingMoviesFromRepository() = runBlocking {
val viewModel = mockSuccessfulCaseUpcomingMovies()
assertEquals(upcomingMoviesExpected, viewModel.upcomingMovies.getValueForTest())
}
private fun mockSuccessfulCaseUpcomingMovies(): MoviesViewModel {
runBlocking {
whenever(repository.getUpcomingMovies(1)).thenReturn(
flow {
emit(upcomingMoviesExpected)
}
)
}
return MoviesViewModel(repository)
}
}
And viewModel:
class MoviesViewModel(
private val repository: MoviesRepository
): ViewModel() {
val upcomingMovies: MutableLiveData<UpcomingResponse> = MutableLiveData()
var upcomingMoviesPage = 0
private var upcomingMoviesResponse: UpcomingResponse? = null
init {
getUpcomingMovies()
}
fun getUpcomingMovies() = viewModelScope.launch {
upcomingMoviesPage++
repository.getUpcomingMovies(upcomingMoviesPage).collect { result ->
if (result.isSuccess) {
result.getOrNull()!!.body()?.let {
if (upcomingMoviesResponse == null) {
upcomingMoviesResponse = it
} else {
val oldMovies = upcomingMoviesResponse?.results
val newMovies = it.results
oldMovies?.addAll(newMovies)
}
upcomingMovies.postValue(upcomingMoviesResponse ?: it)
}
}
}
}
}
And the result is:
expected:<Success(Mock for Response, hashCode: 1625939772)> but was:<null>
Expected :Success(Mock for Response, hashCode: 1625939772)
Actual :null

Kotlin StateFlow onStart unit test

I'm trying to unit test my ViewModel. It has a state which is of the type StateFlow.
When I call my useCase, I set the value of the state to Loading in the onStart block as follows:
#HiltViewModel
class MyViewModel #Inject constructor(private val myUseCase: MyUseCase) : ViewModel() {
// ...
private val _state: MutableStateFlow<MyState> = MutableStateFlow(MyState.Loading)
val state = _state.asStateFlow()
init {
loadData()
}
fun loadData() {
viewModelScope.launch {
myUseCase().onStart {
_state.value = MyState.Loading
}.collect { data ->
// ...
_state.value = MyState.Data(data)
}
}
}
// ...
}
I'm trying to test that the first emitted value from the flow is the Loading state but my test fails and says expected Data not Loading.
Here is my test class:
class MyViewModelTest {
private lateinit var viewModel: MyViewModel
private lateinit var fakeUseCase: MyFakeUseCase
#BeforeEach
fun setup() {
fakeUseCase = MyFakeUseCase()
viewModel = MyViewModel(fakeUseCase)
}
#Test
fun loadData_anyCriteria_stateIsLoadingOnStart() {
runBlocking {
// Act
viewModel.loadData()
val result = viewModel.state.first()
// Assert
Assertions.assertEquals(result, MyState.Loading)
}
}
}
Test result:
Expected :Data(bla bla bla)
Actual :com.example....MyState$Loading#7b8233cd
How can I test the first value emitted from a StateFlow?

How do I invoke a callback in a unit test in Android?

I want to invoke a callback to assert the execution it makes.
I'm using MVVM in my app. In one of the view models I implemented, I want to make sure the ui state changes when a process is completed.
In my HomeViewModel.kt I have:
#HiltViewModel
class HomeViewModel
#Inject
constructor(
private val storageRepository: StorageRepository,
private val accountRepository: AccountRepository,
) : ViewModel() {
// First state of isLoading is true
var uiState = mutableStateOf(HomeUiState())
...
fun addListener() {
viewModelScope.launch {
storageRepository.addListener(
accountRepository.getUserId(),
::onDocumentEvent,
onComplete = {
uiState.value = uiState.value.copy(isLoading = false)
},
onError = {
error -> onAddListenerFailure(error)
}
)
}
}
And I want to write the test:
Given homeViewModel.addListener()
When storageRepository.addListener(...) completes
Then uiState.isLoading is false
I've been searching for some time now and I have found some people referring to using captors from mockito but nothing that applies to my case.
This is what I have now
#OptIn(ExperimentalCoroutinesApi::class)
internal class HomeViewModelTest {
// mock repositories
#Mock lateinit var storageRepository: StorageRepository
#Mock lateinit var accountRepository: AccountRepository
#Mock lateinit var logRepository: LogRepository
// set dispatcher to be able to run tests
private val dispatcher = StandardTestDispatcher()
lateinit var callbackCaptor: KArgumentCaptor<() -> Unit>
#Before
fun setUp() {
MockitoAnnotations.openMocks(this)
Dispatchers.setMain(dispatcher)
}
#After
fun tearDown() {
Dispatchers.resetMain()
}
#Test
fun `loading state is true when viewModel is created`() {
val homeViewModel = HomeViewModel(storageRepository, accountRepository, logRepository)
assertTrue(homeViewModel.uiState.value.isLoading)
}
#Test
fun `loading state is false when listener is added successfully`() {
val homeViewModel = HomeViewModel(storageRepository, accountRepository, logRepository)
callbackCaptor = argumentCaptor()
whenever(
storageRepository.addListener(
anyString(),
anyOrNull(),
callbackCaptor.capture(),
anyOrNull()
)
)
.thenAnswer { callbackCaptor.firstValue.invoke() }
homeViewModel.addListener()
// wait for mutable state to update
dispatcher.scheduler.advanceUntilIdle()
assertFalse(homeViewModel.uiState.value.isLoading)
}
}
Of course, I'm open to hearing solutions using something else than captors.
I think you are not initialising the captor, try following
#Test
fun `loading state is false when listener completes its process`() {
val homeViewModel = HomeViewModel(storageRepository, accountRepository, logRepository)
val callbackCaptor = argumentCaptor<() -> Unit>() //used kotlin mockito
whenever(storageRepository.addListener(anyString(), any(), callbackCaptor.capture(), any()))
.thenAnswer { callbackCaptor.firstValue.invoke() }
homeViewModel.addListener()
// wait for mutable state to update
dispatcher.scheduler.advanceUntilIdle()
assertFalse(homeViewModel.uiState.value.isLoading)
}

Mocked multiple times, but mocked value does not change after secondly mocked in different test method

I'm writing unit test for my ViewModel. I have mocked my data source and want to test that datasource returns success and error cases. If i run tests individually everything is OK.
In the first method i mocked to return success, in the second method i mocked to return error. When i run these 2 tests together (by clicking run tests in class name), in the second method i want dataSource.getPackageCard() to return ResponseState.Error("error1337") however it returns ResponseState.Success(responseDto). In other words, it remembers the mocked value from 1st method. Why ? How to solve that problem ?
#MediumTest
#RunWith(AndroidJUnit4::class)
#ExperimentalCoroutinesApi
class MyViewModelTest {
#get: Rule
var instantExecutorRule = InstantTaskExecutorRule()
#get: Rule
var mainCoroutineRule = MainCoroutineRule()
private lateinit var viewModel: MyViewModel
lateinit var MyRepository: MyRepository
val responseDto = MyResponseDto().apply {
val myList = mutableListOf<CardListGroupDTO>()
myList.add(CardListGroupDTO(cardGroupType = "test",
headerTitle = "test",
buttonAll = ButtonDto(title = "test", url = "test")
))
groupList = myList
}
#MockK
lateinit var dataSource: MyDataSource
#Before
fun setup() {
MockKAnnotations.init(this)
MyRepository = MyRepositoryImpl.getInstance(dataSource)
viewModel = MyViewModel(MyRepository)
}
#After
fun afterTests() {
unmockkAll()
unmockkObject(dataSource)
}
#Test
fun `test successful case`() = runBlockingTest {
// given
coEvery {
dataSource.getPackageCard()
} returns ResponseState.Success(
responseDto
)
var counter = 0
viewModel.MyResponseDto.observeForever(
object : Observer<ResponseState<MyResponseDto>> {
override fun onChanged(t: ResponseState<MyResponseDto>) {
// println(viewModel.MyResponseDto.value)
when (counter) {
0 ->
Truth.assertThat(t).isEqualTo(ResponseState.Loading(true))
1 ->
Truth.assertThat(t).isEqualTo(ResponseState.Success(responseDto))
2 -> {
Truth.assertThat(t).isEqualTo(ResponseState.Loading(false))
viewModel.MyResponseDto.removeObserver(this)
}
}
counter++
}
})
viewModel.getPackageCard()
}
#Test
fun `test error case`() = runBlockingTest {
val errorMessage = "error1337"
// given
coEvery {
dataSource.getPackageCard()
} returns ResponseState.Error(
errorMessage
)
var counter = 0
viewModel.MyResponseDto.observeForever(
object : Observer<ResponseState<MyResponseDto>> {
override fun onChanged(t: ResponseState<MyResponseDto>) {
// println(viewModel.MyResponseDto.value)
when (counter) {
0 ->
Truth.assertThat(t).isEqualTo(ResponseState.Loading(true))
1 ->
Truth.assertThat(t).isEqualTo(ResponseState.Error(errorMessage))
2 -> {
Truth.assertThat(t).isEqualTo(ResponseState.Loading(false))
viewModel.MyResponseDto.removeObserver(this)
}
}
counter++
}
})
viewModel.getPackageCard()
}
}
I found the answer finally. Since i use static repository MyRepositoryImpl.getInstance(dataSource), the datasource is mocked once. Second mock is not valid. I did manual singleton, inside companioan object create if it is not null, if nonnull return the object. This is the cause of my problem.
I solved the problem, by removing the singleton pattern i implemented as the above. I used constructor injection and made my repository singleton in this way. In my unit tests my repository is not singleton any more.
#Singleton
class MyRepositoryImpl #Inject constructor(
private val myRemoteDataSource: MyRemoteDataSource
) : MyRepository
And my viewmodel test is fixed when i write the following :
#Before
fun setup() {
MockKAnnotations.init(this)
myRepository = MyRepositoryImpl(dataSource)
viewModel = MyViewModel(myRepository)
}

Android Unit Test: How to mock an object which contains MutableLiveData but only exposes LiveData?

I have a repository class that uses a MutableLiveData object, exposed as just LiveData, to return results of async web queries to a ViewModel. The ViewModel then uses Transformation to map the results to another MutableLiveData which is observed by a View.
I think I followed the recommended architecture in this module by separating the concerns, but I find it hard to write unit tests for the ViewModel:
class DataRepository ( private val webservice: DataWebService ) {
private val _exception = MutableLiveData<Exception?>(null)
val exception : LiveData<Exception?> get() = _exception
private val _data = MutableLiveData<List<DataItem>>()
val data: LiveData<List<DataItem>> = _data
private val responseListener = Response.Listener<String> {response ->
try {
val list = JsonReader(SearchResult.mapping).readObject(response).map {
//Data transformation
}
_exception.value = null
_data.value = list
} catch (ex: Exception) {
_exception.value = ex
_data.value = emptyList()
}
}
fun findData(searchString: String) {
_data.value = emptyList()
webservice.findData(searchString, responseListener = responseListener)
}
}
class WebServiceDataViewModel (private val repository: DataRepository, app: App) : AndroidViewModel(app)
{
val dataList: LiveData<List<DataItem>> = Transformations.map(repository.data) {
_showEmpty.value = it.isEmpty()
it
}
val exception: LiveData<Exception?> get() = repository.exception
private val _showEmpty = MutableLiveData(true)
val showEmpty : LiveData<Boolean> = _showEmpty
private var _reloadOnCreate = true
var searchString: String? = null
set(value) {
field = value
if (!value.isNullOrBlank()) {
repository.findData(value)
}
}
}
ViewModel Test class:
#RunWith(JUnit4::class)
class WebServicePodcastViewModelTest {
#Rule var instantExecutorRule = InstantTaskExecutorRule()
#Mock lateinit var repository : DataRepository
#Mock lateinit var app : App
lateinit var viewModel: WebServiceDataViewModel
#Mock lateinit var exceptionObserver : Observer<Exception?>
#Mock lateinit var dataObserver : Observer<List<DataItem>>
#Mock lateinit var showEmptyObserver : Observer<Boolean>
#Before
fun setUp() {
MockitoAnnotations.initMocks(this)
viewModel = WebServiceDataViewModel(repository, app)
viewModel.exception.observeForever(exceptionObserver)
viewModel.showEmpty.observeForever(showEmptyObserver)
viewModel.dataList.observeForever(dataObserver)
}
#Test
fun searchForData() {
//given
val searchString = "MockSearch"
//when
`when`(repository.findData(searchString)).then { /* How to invoke the mock repositories LiveData? */ }
//then
//TODO: verify that ViewModel LiveData behaves as expected
}
}
So how do I invoke the immutable LiveData of a mocked class? Currently I'm trying to use Mockito & JUnit, but I'm open to different frameworks if the setup is easy!
In the end I ditched Mockito and used MockK instead which works like a charm!
You must take into consideration that you need to set and observe in the test the livedata which is observed from the view, then you can do something like this:
(As good practice, instead of having different Livedatas depending on the type od result, you can use a Sealed Class with generics to encapsulate your UI's state and to facilitate the process of observing different livedata rather than just one)
#RunWith(MockitoJUnitRunner::class)
class WebServicePodcastViewModelTest {
#Rule var instantExecutorRule = InstantTaskExecutorRule()
#Mock lateinit var repository : DataRepository
#Mock lateinit var app : App
lateinit var viewModel: WebServiceDataViewModel
#Mock lateinit var exceptionObserver : Observer<Exception?>
#Mock lateinit var dataObserver : Observer<List<DataItem>>
#Mock lateinit var showEmptyObserver : Observer<Boolean>
#Before
fun setUp() {
viewModel = WebServiceDataViewModel(repository, app)
viewModel.exception.observeForever(exceptionObserver)
viewModel.showEmpty.observeForever(showEmptyObserver)
viewModel.dataList.observeForever(dataObserver)
}
#Test
fun searchForData() {
//given
val searchString = "MockSearch"
val dataResponse = MutableLiveData<List<DataItem>>()
dataResponse.value = listOf<DataItem>()
//when
`when`(repository.findData(searchString)).then {
dataResponse
}
//then
assertNotNull(viewModel.getDataList().value)
assertEquals(viewModel.getDataList().value, emptyList()) /* empty list must be an object of the same type of listOf<DataItem>() */
}
}

Categories

Resources