2

I want to use spyk for class with generic. It produces StackOverflowError. It is impossible to use every { childClazz.foobar(view) } just Runs and super.foobar(view) definitely need to be invoked

class SpyTest{
    @Test
    fun  callFoobar(){
        val view = mockk<SomeView>()
        val childClazz = spyk(ChildClazz(), recordPrivateCalls = true)
        childClazz.foobar(view)
        assertk.assert(childClazz.called).isEqualTo(true)
    }
}

private interface ParentView

private interface SomeView : ParentView

private interface ParentInterface<V : ParentView> {

    fun foobar(view: V)
}

private abstract class ParentClazz<V : ParentView> : ParentInterface<V> {

    override fun foobar(view: V) {

    }
}

private class ChildClazz : ParentClazz<SomeView>() {

    var called: Boolean = false

    override fun foobar(view: SomeView) {
        super.foobar(view)
        called = true
    }
} 

Here is a stacktrace:

java.lang.StackOverflowError
    at java.lang.Class.getDeclaredMethod(Class.java:2127)
    at java.lang.Object.equals(Object.java:149)
    at kotlin.jvm.internal.Intrinsics.areEqual(Intrinsics.java:164)
    at kotlin.reflect.jvm.internal.KClassCacheKt.getOrCreateKotlinClass(kClassCache.kt:36)
    at kotlin.reflect.jvm.internal.ReflectionFactoryImpl.getOrCreateKotlinClass(ReflectionFactoryImpl.java:45)
    at kotlin.jvm.internal.Reflection.getOrCreateKotlinClass(Reflection.java:61)
    at kotlin.jvm.JvmClassMappingKt.getKotlinClass(JvmClassMapping.kt:91)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper.toDescription(JvmMockFactoryHelper.kt:55)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$toDescription(JvmMockFactoryHelper.kt:13)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invoke(JvmMockFactoryHelper.kt:17)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invoke(JvmMockFactoryHelper.kt:13)
    at io.mockk.impl.instantiation.JvmMockFactoryKt$sam$MockKInvocationHandler$4dff1f07.invocation(JvmMockFactory.kt)
    at io.mockk.proxy.MockKCallProxy.call(MockKCallProxy.java:24)
    at com.app.ChildClazz.foobar(SimpleTest.kt:62)
    at com.app.ChildClazz.foobar(SimpleTest.kt:55)
    at sun.reflect.GeneratedMethodAccessor12.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

Is it possible to use spyk here? I need it for private methods.

Paraskevas Ntsounos
  • 1,755
  • 2
  • 18
  • 34
  • A better place for such reports is GitHub issues, as you can be sure whether it is fixed or not. I created one: https://github.com/mockk/mockk/issues/81 – oleksiyp Jun 01 '18 at 18:34
  • The issue is bridge method handling, I'll fix it, but it may be not so easy, so won't be fast. – oleksiyp Jun 01 '18 at 18:44

0 Answers0