Mocktito ArgumentCaptor为Kotlin lambda提供参数 [英] Mocktito ArgumentCaptor for Kotlin lambda with arguments
问题描述
verify(myInterface).doSomething(argumentCaptor.capture())
capture.value.invoke(0L)
其中doSomething是:
doSomething((Long) - > Unit)
如何为此创建一个ArgumentCaptor?现在我正在做这件事
内嵌乐趣< reified T:Any> argumentCaptor()= ArgumentCaptor.forClass(T :: class.java)!!
val captor = argumentCaptor<(Long) - > ())
$ b verify
但是我得到java.lang.IllegalStateException:captor.capture()不能为空
我也尝试过整合mockito-kotlin,但是我得到一个PowerMockito错误:
没有找到名为reported的实例字段org.mockito.internal.MockitoCore的类层次结构。
使用 mockito-kotlin 就像这样:
val myService = mock< MyInterface>()
pre>
myService.doSomething {
println(it)
}
verify(myService).doSomething(capture {function - >
function.invoke(123)
})
编辑:删除不必要的
argumentCaptor<(Long) - > Unit>()。apply {}
- 它没有被使用I am trying to test this on Kotlin:
verify(myInterface).doSomething(argumentCaptor.capture()) capture.value.invoke(0L)
Where doSomething is:
doSomething((Long) -> Unit)
How can I create an ArgumentCaptor for this? Right now I am doing this
inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)!! val captor = argumentCaptor<(Long) -> Unit>() verify(mainApiInterface!!).downloadUserProfilePicture(captor.capture()) captor.value.invoke(0L)
But I am getting java.lang.IllegalStateException: captor.capture() must not be null
I also tried integrating mockito-kotlin but I get a PowerMockito error:
No instance field named "reported" could be found in the class hierarchy of org.mockito.internal.MockitoCore.
解决方案Using mockito-kotlin like this seems to work:
val myService = mock<MyInterface>() myService.doSomething { println(it) } verify(myService).doSomething(capture { function -> function.invoke(123) })
Edit: removed unnecessary
argumentCaptor<(Long) -> Unit>().apply {}
- it wasn't used这篇关于Mocktito ArgumentCaptor为Kotlin lambda提供参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!