mockk icon indicating copy to clipboard operation
mockk copied to clipboard

Can't Instantiate Proxy for Class with Method Using NonEmptySet as a Parameter

Open biubiu opened this issue 10 months ago • 4 comments

mockk version: 1.13.17 kotest version : "5.9.1" kotlin version "2.0.21" arrow version "1.2.4"

Current Behavior

When using mockk , when mocked interface having method with NonEmptySet(arrow.core) as parameter, it errors Can't instantiate proxy for ... error

Test Class

import arrow.core.NonEmptySet
import arrow.core.nonEmptySetOf
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.mockk.coEvery
import io.mockk.mockk

class MockkBugTest: FunSpec(){
  init {
    test("should run") {
      val someService = mockk<SomeInterface>(){
        coEvery { someFunction(any()) } returns "abc"
      }
      val caller = SomeCaller(someService)
      caller.callSomeFunction(nonEmptySetOf("a", "b", "c")) shouldBe "abc"
    }
  }
}

class SomeCaller(val someInterface: SomeInterface){
  fun callSomeFunction(sets: NonEmptySet<String>){
    someInterface.someFunction(sets)
  }
}

interface SomeInterface {
    fun someFunction(something: NonEmptySet<String>): String
}

class SomeClass : SomeInterface {
    override fun someFunction(something: NonEmptySet<String>): String = something.reduce { acc, s -> acc + s }
}

Error

Can't instantiate proxy for SomeInterface
io.mockk.MockKException: Can't instantiate proxy for class SomeInterface
	at io.mockk.impl.instantiation.JvmMockFactory.newProxy(JvmMockFactory.kt:64)
	at io.mockk.impl.instantiation.AbstractMockFactory.newProxy$default(AbstractMockFactory.kt:24)
	at io.mockk.impl.instantiation.AbstractMockFactory.mockk(AbstractMockFactory.kt:59)

biubiu avatar Mar 13 '25 01:03 biubiu

We have the same issue. The issue appeared when updating the mokk version from 1.13.9 to 1.13.17.

NekiyXX avatar Apr 16 '25 09:04 NekiyXX

I have the same problem after the update

val arrowVersion = "2.0.1"
val mockkVersion = "1.13.17"

With arrow, Nel (NonEmptyList)

aitabakov avatar Apr 16 '25 10:04 aitabakov

same problem with version 1.14.2

spent some time to realize It was this type since the exception doesn't mention It.

filipeportes avatar May 15 '25 10:05 filipeportes

Hi Isn't it because of type erasure and the inlining a value class?

@JvmInline
value class Box<T>(val items: List<T>)

interface SomeInterface {
    val box: Box<String>
}

class MyTest {
    @Test
    fun myTest() {
        val someService = mockk<SomeInterface>() // fails
    }
}

I don't think it's doable.

VitalyVPinchuk avatar May 22 '25 19:05 VitalyVPinchuk