Mocktito ArgumentCaptor为Kotlin lambda提供参数 [英] Mocktito ArgumentCaptor for Kotlin lambda with arguments

查看:1155
本文介绍了Mocktito ArgumentCaptor为Kotlin lambda提供参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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>()

myService.doSomething {
println(it)
}

verify(myService).doSomething(capture {function - >
function.invoke(123)
})
pre>

编辑:删除不必要的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆