Does anyone here have any experience mocking an ex...
# server
j
Does anyone here have any experience mocking an extension function and capturing the lambda parameter? I am using Mockk and I'm getting this error
Copy code
java.lang.AbstractMethodError: Receiver class kotlin.jvm.functions.Function1$Subclass0 does not define or inherit an implementation of the resolved method 'abstract java.lang.Object invoke(java.lang.Object)' of interface kotlin.jvm.functions.Function1.
Here's my test:
Copy code
@Test
    fun `attempt 2`() {
        val routing = mockk<Routing>(relaxed = true)

        val configure = slot<Route.() -> Unit>()
        val mockedRoute = mockk<Route>()

        every { any<Route>().static(capture(configure)) } answers {
            configure.captured.invoke(mockedRoute)
            mockedRoute
        }

        var testObj = ResourcePortalModuleRouter(routing, node)
        testObj.attachPortalCoreRoutes()

        verify {
            mockedRoute.default("index")
        }
    }
The error is thrown on the
every
line, and I don't understand why this isn't working
t
Usually, if you want to mock static/top level functions, you need to register it with
mockkstatic("<file class qualified name>")
, so if you have a file called
MyExtensions.kt
in package
com.example.myapp
you would write
mockkstatic("com.example.myapp.MyExtensionsKt")
. I'm not 100% sure about mocking extensions in combination with the any-matcher, but that shouldn't make a difference when I think about it.
e
you should be able to
mockkStatic(Routing::static)
instead of relying on the class name
👍🏻 1
j
Awesome, thanks all for the help. I will give mockkStatic a try
659 Views