我具有以下Kotlin暂停功能:
suspend fun registerNewTransaction(transaction: String): String
我正在尝试通过Spock对其进行测试,但是由于Groovy将kotlin容器类标识为Java类,因此我没有找到在不指定Continuation<? super String>
参数的情况下调用此kotlin悬挂函数的方法。
我想知道是否可以从另一种基于JVM的语言中调用挂起函数,否则我将不得不使用另一种测试库,例如JUnit。
答案 0 :(得分:0)
如果可以从另一个基于JVM的调用挂起函数 语言
可能,但是您需要传递Continuation
作为最后一个参数,因为这是Kotlin编译suspend
函数的方式
我建议使用Kotlin测试Kotlin代码,有很多支持协程的Kotlin特定库,例如mockk
。
答案 1 :(得分:0)
您可以模拟Continuation对象,该对象被kotlin编译到方法调用中。这段代码对我有用:
// mocked continuation
def continuation = Mock(Continuation) {
getContext() >> Mock(CoroutineContext)
}
// method call which should be tested
subjectUnderTest.suspendableMethod(someParameter, continuation)