I tried to test a suspend function got the error:
Cannot invoke "android.os.Looper.getThread()" because the return value of "android.os.Looper.getMainLooper()" is null
class HomeViewModelTest {
@Test
fun settingMainDispatcher() = runTest {
val testDispatcher = UnconfinedTestDispatcher(testScheduler)
Dispatchers.setMain(testDispatcher)
try {
//My test code is here
myMethod()
} finally {
Dispatchers.resetMain()
}
}
}
suspend fun myMethod() {
withContext(Dispatchers.Main) {
}
}
Android Studio Version: Android Studio Koala | 2024.1.1