mockito 想要但没有被调用,实际上与这个 mock 的交互为零
mockito wanted but not invoked, Actually there were zero interactions with this mock
我为这个错误再次 post 道歉,但我仔细检查了另一个 post 并确保它不是同一个原因。
我有一个使用 ExploreService
的 ExploreRepository
class。存储库实现 NetworkBoundResource。我是单元测试仓库,使用Mockito模拟服务。
class ExploreRepository(
private val context: Context,
private val exploreService: ExploreService
) {
suspend fun getProperties(southwest: LatLng, northeast: LatLng, zoomLevel: Float): LiveResource<List<Property>> {
return object : NetworkBoundResource<List<Property>, SearchProperties>() {
override suspend fun makeApiCall(): SearchProperties {
println("exploreService hashcode ${System.identityHashCode(exploreService)}")
return exploreService.getProperties(context, geoHashList, precision)
}
}.asLiveResource()
}
我的 testExploreRepository class
@Config(sdk = intArrayOf(Build.VERSION_CODES.O_MR1))
@RunWith(AndroidJUnit4::class)
class ExploreRepositoryTest {
...
// Subject under test
private lateinit var exploreRepository: ExploreRepository
// Use a fake service to be injected into the repository
private lateinit var mockExploreService: ExploreService
private lateinit var mockSearchProperties: SearchProperties
private val context: Application = ApplicationProvider.getApplicationContext()
@Before
fun setup(){
mockExploreService = mock()
exploreRepository = ExploreRepository(context, mockExploreService)
mockSearchProperties = mock()
}
@Test
fun `should make network service call`() = runBlocking<Unit> {
// stubbing
whenever(mockExploreService.getProperties(any(), any(), any())).thenReturn(mockSearchProperties)
val northeast = LatLng( 37.799001, -122.422889)
val southwest = LatLng( 37.782180, -122.451554)
val zoomLevel = 10f
exploreRepository.getProperties(southwest,northeast,zoomLevel)
println("exploreService hashcode ${System.identityHashCode(mockExploreService)}") // debug line
verify(mockExploreService, times(1)).getProperties(any(), any(), any())
}
}
我已经打印出 exploreService 的 hashCode。
makeApiCall() 中的 println("exploreService hashcode ${System.identityHashCode(exploreService)}")
做了 运行。
而且我确保我在我的存储库中调用相同的模拟服务实例。
但是 mockito 仍然抛出 Actually, there were zero interactions with this mock.
编辑:实际上在测试文件中,启用了 println() 调试行。错误消失了。没有它会抛出错误。有什么线索吗?
我认为您正在尝试使用协程测试策略来测试回调实现。因此,测试可能会在回调内部调用模拟之前完成。此外,您正在 return 从挂起方法中获取“LiveResource”,因此也许您应该使用它并在 return 上进行验证而不是检查模拟,因为您确实在方法中创建了一个对象
我为这个错误再次 post 道歉,但我仔细检查了另一个 post 并确保它不是同一个原因。
我有一个使用 ExploreService
的 ExploreRepository
class。存储库实现 NetworkBoundResource。我是单元测试仓库,使用Mockito模拟服务。
class ExploreRepository(
private val context: Context,
private val exploreService: ExploreService
) {
suspend fun getProperties(southwest: LatLng, northeast: LatLng, zoomLevel: Float): LiveResource<List<Property>> {
return object : NetworkBoundResource<List<Property>, SearchProperties>() {
override suspend fun makeApiCall(): SearchProperties {
println("exploreService hashcode ${System.identityHashCode(exploreService)}")
return exploreService.getProperties(context, geoHashList, precision)
}
}.asLiveResource()
}
我的 testExploreRepository class
@Config(sdk = intArrayOf(Build.VERSION_CODES.O_MR1))
@RunWith(AndroidJUnit4::class)
class ExploreRepositoryTest {
...
// Subject under test
private lateinit var exploreRepository: ExploreRepository
// Use a fake service to be injected into the repository
private lateinit var mockExploreService: ExploreService
private lateinit var mockSearchProperties: SearchProperties
private val context: Application = ApplicationProvider.getApplicationContext()
@Before
fun setup(){
mockExploreService = mock()
exploreRepository = ExploreRepository(context, mockExploreService)
mockSearchProperties = mock()
}
@Test
fun `should make network service call`() = runBlocking<Unit> {
// stubbing
whenever(mockExploreService.getProperties(any(), any(), any())).thenReturn(mockSearchProperties)
val northeast = LatLng( 37.799001, -122.422889)
val southwest = LatLng( 37.782180, -122.451554)
val zoomLevel = 10f
exploreRepository.getProperties(southwest,northeast,zoomLevel)
println("exploreService hashcode ${System.identityHashCode(mockExploreService)}") // debug line
verify(mockExploreService, times(1)).getProperties(any(), any(), any())
}
}
我已经打印出 exploreService 的 hashCode。
makeApiCall() 中的 println("exploreService hashcode ${System.identityHashCode(exploreService)}")
做了 运行。
而且我确保我在我的存储库中调用相同的模拟服务实例。
但是 mockito 仍然抛出 Actually, there were zero interactions with this mock.
编辑:实际上在测试文件中,启用了 println() 调试行。错误消失了。没有它会抛出错误。有什么线索吗?
我认为您正在尝试使用协程测试策略来测试回调实现。因此,测试可能会在回调内部调用模拟之前完成。此外,您正在 return 从挂起方法中获取“LiveResource”,因此也许您应该使用它并在 return 上进行验证而不是检查模拟,因为您确实在方法中创建了一个对象