using mockk for testing kotlin function.

private val serviceObject = mockk<Service>()
private val serviceToBeTested = ServiceToBeTestd(Service)
        
fun test(){
    when(serviceObject.function1(argument1,argument1))
        .thenReturn(<something>)
}

When i try to run it, i get this error:

io.mockk.MockKException: no answer found for: Service(#1).function1(argument1, argument2)

Any idea why ?

ServiceToBeTest是要测试的服务,服务通过有线连接:

open class ServiceToBeTestd
    constructor(private val service: Service)

推荐答案

You are using mockito syntax.

下面是mockk的正确语法.

val car = mockk<Car>()

every { car.drive(Direction.NORTH) } returns Outcome.OK

Please update your syntax.

Kotlin相关问答推荐

了解Kotlin函数

Spring Boot kotlin协程不能并行运行

同时也是一个字符串的 Kotlin 枚举

协程子作业(job)取消

Kotlin 插件之间的区别

我什么时候可以在 LazyList 或 Column 的范围内使用 Composable?

SpringBoot 2.5.0 对于 Jackson Kotlin 类的支持,请在类路径中添加com.fasterxml.jackson.module: jackson-module-kotlin

什么是开放式property?为什么我不能将其设置器设为private私有?

当被Spring代理类访问时,Kotlin实例变量为null

参数不匹配;SimpleXML

如何在Kotlin中获得KType?

Kotlin中的Memoization功能

如何在使用Koin DI的活动之间共享同一个ViewModel实例?

在 sharedPref.getString 中有一个默认值有什么意义?

如何将 CameraView 与 Jetpack Compose 一起使用?

在android java类中使用Kotlin扩展

WebFlux 功能:如何检测空 Flux 并返回 404?

Kotlin var lazy init

项目不会使用 Kotlin 1.1.3 构建

Kotlin中默认导入哪些包/函数?