How to mock a method with lambda argument, e.g. `...
# mockk
d
How to mock a method with lambda argument, e.g.
AsyncTaskExecutor#submit
? Currently I'm doing this
Copy code
val taskExecutor: AsyncTaskExecutor =
        mockk {
            every { submit(captureLambda()) } answers {
                CompletableFuture.completedFuture(lambda<() -> Int>().invoke())
            }
        }
but getting
Copy code
Caused by: java.lang.ClassCastException: class java.lang.Object cannot be cast to class kotlin.Function (java.lang.Object is in module java.base of loader 'bootstrap'; kotlin.Function is in unnamed module of loader 'app')
I should probably use
hint
but not sure how exactly.
@Mattia Tommasone Do you have any idea how lambda arguments should be captured invoked? Sorry for direct mention 🙂
I see the problem now.
captureLambda()
is intended only for Kotlin lambdas (which implement the
Function
interface). For functional interfaces (
Runnable
in my case) one must use the generic
capture(slot)
technique.
494 Views