当我尝试在 kotlin 中进行单元测试时,在 android.os.Looper 中使用 kotlinMethod myLooper 中的协同程序进行单元测试不是模拟错误
Unit test with coroutines in kotlinMethod myLooper in android.os.Looper not mocked error when I try to Unit test in kotlin
当我尝试使用协程在 kotlin 中测试我的 ViewModel 时,出现 Method myLooper in android.os.Looper not mocked
错误。
有 ViewModel
class MainViewModel(private val uiContext: CoroutineContext = Dispatchers.Main) : ViewModel(), CoroutineScope {
private val heroesRepository: HeroesRepository = heroesRepositoryModel.instance()
val data = MutableLiveData<List<Heroes.Hero>>()
private var job: Job = Job()
override val coroutineContext: CoroutineContext
get() = uiContext + job
fun getHeroesFromRepository(page: Int) {
launch {
try {
val response = withContext(Dispatchers.IO) {
heroesRepository.getHeroes(page).await()
}
data.value = response.data.results
} catch (e: HttpException) {
data.value = null
} catch (e: Throwable) {
data.value = null
}
}
}
override fun onCleared() {
super.onCleared()
job.cancel()
}
}
以及我为此 ViewModel 所做的测试
class HeroesDataSourceTest {
@Mock
lateinit var heroesRepository: HeroesRepository
@Mock
lateinit var deferred: Deferred<Heroes.DataResult>
val hero = Heroes.Hero(1, "superman", "holasuperman", 1, null, null)
val results = Arrays.asList(hero)
val data = Heroes.Data(results)
val dataResult = Heroes.DataResult(data)
@Before
fun initTest() {
MockitoAnnotations.initMocks(this)
}
@Test
fun testLoadInitialSuccess(): Unit = runBlocking {
`when`(heroesRepository.getHeroes(0)).thenReturn(deferred)
`when`(deferred.await()).thenReturn(dataResult)
var liveData: MutableLiveData<List<Heroes.Hero>>
val mainViewModel = MainViewModel(Dispatchers.Unconfined)
liveData = mainViewModel.data
mainViewModel.getHeroesFromRepository(0)
delay(10000L)
Assert.assertEquals(dataResult, liveData.value)
}
}
我对其进行了调试,它在 ViewModel 的 data.value = response.data.results
行中给出了错误。它会出现异常,但可以肯定的是,由于数据为空,assertEquals 将为假。
我检查了这个线程:
还有这个解决方案:https://android.jlelse.eu/mastering-coroutines-android-unit-tests-8bc0d082bf15
这行得通,但在 kotlin 1.3 中 kotlinx.coroutines.experimental.android.UI
行不通。
LiveData 在内部使用 MainLooper。添加此依赖项(或其支持库版本):
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
这条规则:
@get:Rule
val instantExecutorRule = InstantTaskExecutorRule()
https://developer.android.com/reference/android/arch/core/executor/testing/InstantTaskExecutorRule
当我尝试使用协程在 kotlin 中测试我的 ViewModel 时,出现 Method myLooper in android.os.Looper not mocked
错误。
有 ViewModel
class MainViewModel(private val uiContext: CoroutineContext = Dispatchers.Main) : ViewModel(), CoroutineScope {
private val heroesRepository: HeroesRepository = heroesRepositoryModel.instance()
val data = MutableLiveData<List<Heroes.Hero>>()
private var job: Job = Job()
override val coroutineContext: CoroutineContext
get() = uiContext + job
fun getHeroesFromRepository(page: Int) {
launch {
try {
val response = withContext(Dispatchers.IO) {
heroesRepository.getHeroes(page).await()
}
data.value = response.data.results
} catch (e: HttpException) {
data.value = null
} catch (e: Throwable) {
data.value = null
}
}
}
override fun onCleared() {
super.onCleared()
job.cancel()
}
}
以及我为此 ViewModel 所做的测试
class HeroesDataSourceTest {
@Mock
lateinit var heroesRepository: HeroesRepository
@Mock
lateinit var deferred: Deferred<Heroes.DataResult>
val hero = Heroes.Hero(1, "superman", "holasuperman", 1, null, null)
val results = Arrays.asList(hero)
val data = Heroes.Data(results)
val dataResult = Heroes.DataResult(data)
@Before
fun initTest() {
MockitoAnnotations.initMocks(this)
}
@Test
fun testLoadInitialSuccess(): Unit = runBlocking {
`when`(heroesRepository.getHeroes(0)).thenReturn(deferred)
`when`(deferred.await()).thenReturn(dataResult)
var liveData: MutableLiveData<List<Heroes.Hero>>
val mainViewModel = MainViewModel(Dispatchers.Unconfined)
liveData = mainViewModel.data
mainViewModel.getHeroesFromRepository(0)
delay(10000L)
Assert.assertEquals(dataResult, liveData.value)
}
}
我对其进行了调试,它在 ViewModel 的 data.value = response.data.results
行中给出了错误。它会出现异常,但可以肯定的是,由于数据为空,assertEquals 将为假。
我检查了这个线程:
还有这个解决方案:https://android.jlelse.eu/mastering-coroutines-android-unit-tests-8bc0d082bf15
这行得通,但在 kotlin 1.3 中 kotlinx.coroutines.experimental.android.UI
行不通。
LiveData 在内部使用 MainLooper。添加此依赖项(或其支持库版本):
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
这条规则:
@get:Rule
val instantExecutorRule = InstantTaskExecutorRule()
https://developer.android.com/reference/android/arch/core/executor/testing/InstantTaskExecutorRule