I have a ViewModel class which has a init block function(s), each function will be observing for a MutableStateFlow<State>, State is nothing but a sealed class, in which i will be updating the value as sucees, Failure (or) loading. Now i need to create a mock View Model but in my case i am having the init block which has the mutablestate that is being observed, now every time i create mock for view model i am getting as null which not expected for a non-null state value, below is my code, i need to know how we can mock the mutablestate observable and write unit test case for the data in the block.
public class ViewModel(
private val observeJoinDataUseCase: ObserveJoinDataUseCase
) {
init{
subscribeOnJoinData()
}
fun subscribeOnJoinData() {
observeJoinDataUseCase.execute().observe { joinData ->
when (joinData) {
is State.Success -> {
isSelfUserHardMuted = joinData.data.students.find { it.id == studentUserId }
?.micHardMuted == true
if(isSelfUserHardMuted) sharedPreferencesHelper.audioEnabled = false
joinSession(joinData.data.toPresentation())
if (joinData.data.session?.ended == true) {
joinDataFlowSharedFlow.emit(true)
}
}
State.Loading -> {
screenLoadingSharedFlow.emit(State.Loading)
}
is State.Error -> {
apiErrorSharedFlow.emit(State.Cancelled)
}
State.Idle, State.Cancelled, State.StopLoading -> Unit
}
}
}
}
observeJoinDataUseCase is an interface as below
interface ObserveJoinDataUseCase {
fun execute(): MutableStateFlow<State<DataClass>>
}
Guide me on how to create test case for the View Model and how can i perform test for the subscribeOnJoinData Function