cobbal
cobbal

Reputation: 70785

MockK creating invalid objects using a private constructor

Given a wrapper class around a foreign pointer:

class CObject private constructor(private val _internalCPointer: Long) {
    external fun doACThing()
    companion object {
        external fun allocate(): CObject
    }
}

mockK is generating instances of this object where _internalCPointer is 0, leading to segfaults.

How can I tell mockK to use CObject.allocate instead of the constructor?

Upvotes: 1

Views: 263

Answers (1)

cobbal
cobbal

Reputation: 70785

It turns out that mockk is unable to mock external methods. So instead I replaced the class with stub kotlin methods, and then I was able to mock it properly.

class CObject private constructor(private val _internalCPointer: Long) {
    fun doACThing() = __jni_doACThing
    private external fun __jni_doACThing()
    ...
}

I never did figure out how to mock the constructor, but at least CObject can now be used in a mock environment.

Upvotes: 0

Related Questions