1

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?

Botje
  • 26,269
  • 3
  • 31
  • 41
cobbal
  • 69,903
  • 20
  • 143
  • 156
  • How mockk is creating this? Have you tried mocking constructor like mockkConstructor(MockCls::class) every { anyConstructed() } returns .... ? – Demigod Nov 17 '21 at 14:28
  • @Demigod Tried, but that gave me a different error: `Missing calls inside every { ... } block.` – cobbal Nov 17 '21 at 15:36
  • It is not clear without a code that tries to mock it) can you provide some? – Demigod Nov 17 '21 at 16:09

1 Answers1

0

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.

cobbal
  • 69,903
  • 20
  • 143
  • 156