0
0
Android Kotlinmobile~20 mins

ViewModel testing in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ViewModel Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output when LiveData is observed in a ViewModel test?
Given a ViewModel with a LiveData that emits a value, what will the observer receive after the value is set?
Android Kotlin
class MyViewModel : ViewModel() {
  private val _data = MutableLiveData<String>()
  val data: LiveData<String> = _data

  fun updateData(value: String) {
    _data.value = value
  }
}

// In test:
val viewModel = MyViewModel()
var observedValue: String? = null
viewModel.data.observeForever { observedValue = it }
viewModel.updateData("Hello")
AobservedValue will be "Hello"
BobservedValue will be "" (empty string)
CobservedValue will be null
DThe test will throw an exception
Attempts:
2 left
💡 Hint
LiveData notifies observers immediately when value changes.
lifecycle
intermediate
2:00remaining
What happens if you observe LiveData without a LifecycleOwner in a ViewModel test?
In a ViewModel test, what is the effect of calling observeForever on LiveData instead of observe with a LifecycleOwner?
Android Kotlin
val liveData = MutableLiveData<Int>()
liveData.observeForever { println(it) }
liveData.value = 5
AThe observer receives the value 5 immediately
BThe observer never receives any value
CThe code throws a NullPointerException
DThe observer receives the value only when lifecycle is started
Attempts:
2 left
💡 Hint
observeForever does not depend on lifecycle state.
🔧 Debug
advanced
2:30remaining
Why does this ViewModel test fail to observe LiveData updates?
Consider this test code snippet. Why does observedValue remain null after updateData is called?
Android Kotlin
class MyViewModel : ViewModel() {
  private val _data = MutableLiveData<String>()
  val data: LiveData<String> = _data

  fun updateData(value: String) {
    _data.value = value
  }
}

// Test code:
val viewModel = MyViewModel()
var observedValue: String? = null
viewModel.data.observe { observedValue = it }
viewModel.updateData("Test")
AobservedValue is overwritten immediately after assignment
BMutableLiveData value is not set correctly
CupdateData is not called on the main thread
Dobserve requires a LifecycleOwner, which is missing, so observer is not registered
Attempts:
2 left
💡 Hint
observe needs a lifecycle to work properly in tests.
🧠 Conceptual
advanced
2:30remaining
What is the main benefit of using a TestCoroutineDispatcher in ViewModel testing?
Why do we use TestCoroutineDispatcher or similar in ViewModel tests that use coroutines?
AIt automatically fixes all coroutine bugs in the ViewModel
BIt allows controlling coroutine execution and timing for predictable tests
CIt disables coroutine execution to speed up tests
DIt replaces LiveData with a synchronous data source
Attempts:
2 left
💡 Hint
Think about how to make asynchronous code run predictably in tests.
📝 Syntax
expert
3:00remaining
What error does this ViewModel test code produce?
Analyze the following test code snippet. What error will it raise when run?
Android Kotlin
class MyViewModel : ViewModel() {
  private val _count = MutableLiveData<Int>()
  val count: LiveData<Int> = _count

  fun increment() {
    _count.value = (_count.value ?: 0) + 1
  }
}

// Test code:
val viewModel = MyViewModel()
var observedCount: Int? = null
viewModel.count.observeForever { observedCount = it }
viewModel.increment()
viewModel.increment()
AIllegalStateException due to observing LiveData twice
BNullPointerException because _count.value is null initially
CNo error, observedCount will be 2
DCompilation error due to missing import
Attempts:
2 left
💡 Hint
Check how the Elvis operator ?: handles null values.