mockk exception - no answer found for

Parameswar picture Parameswar · Jul 23, 2019 · Viewed 7.2k times · Source

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 ?

ServiceToBeTestd is the service to be tested, Service is wired in it:

open class ServiceToBeTestd
    constructor(private val service: Service)

Answer

Hardik Bambhania picture Hardik Bambhania · Jul 23, 2019

You are using mockito syntax.

Below is correct syntax for mockk.

val car = mockk<Car>()

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

Please update your syntax.