Mar*_*ist 5 junit mockito kotlin kotlinx.coroutines
我有一个使用Mockito模拟的暂停函数,但它返回null
两个项目都使用
'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'
Run Code Online (Sandbox Code Playgroud)
例子1
这是我的测试中模拟返回null
@Test
fun `when gps not enabled observer is notified`() = runBlocking {
// arrange
`when`(suspendingLocationService.getCurrentLocation()).thenReturn(result) // <- when called this returns null
// act
presenter.onStartShopButtonClick()
// assert
verify(view).observer
verify(observer).onPrepareShop()
}
Run Code Online (Sandbox Code Playgroud)
我的演示者中有以下实现
override suspend fun onStartShopButtonClick() {
val result = suspendingLocationService.getCurrentLocation() // <- in my test result is null!!!!!!
view?.apply {
observer?.onPrepareShop()
when {
result.hasGivenPermission == false -> observer?.onStartShop(StoreData(), APIError(APIError.ErrorType.NO_PERMISSION))
result.hasGPSEnabled == false -> observer?.onStartShop(StoreData(), APIError(APIError.ErrorType.GPS_NOT_ENABLED))
result.latitude != null && result.longitude != null ->
storeLocationService.getCurrentStore(result.latitude, result.longitude) { store, error ->
observer?.onStartShop(store, error)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
however I have what I believe to a very similar implementation that is working below
Example 2
The below test does pass and the correct the function does respond with a product
@Test
fun `suspending implementation updates label`() = runBlocking {
// arrange
`when`(suspendingProductProvider.getProduct("testString")).thenReturn(product)
// act
presenter.textChanged("testString")
// assert
verify(view).update(product.name)
}
Run Code Online (Sandbox Code Playgroud)
here is the implementation of the presenter
override suspend fun textChanged(newText: String?) {
val product = suspendingNetworkProvider.getProduct(newText)
view?.update(product.name)
}
Run Code Online (Sandbox Code Playgroud)
here is the interface I am mocking
interface SuspendingProductProvider {
suspend fun getProduct(search: String?): Product
}
Run Code Online (Sandbox Code Playgroud)
what I am not doing in the first example
首先获取Mockito-kotlin并在mockito 中,当您想要模拟挂起函数时可以使用此代码:
val mockedObject: TestClass = mock()
mockedObject.stub {
onBlocking { suspendFunction() }.doReturn(true)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
995 次 |
| 最近记录: |