2

我想将spyk用于泛型类。它会产生 StackOverflowError。不可能使用每个 { childClazz.foobar(view) } 只是 Runs并且super.foobar(view)肯定需要被调用

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
    }
} 

这是一个堆栈跟踪:

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)

可以在这里使用spyk吗?我需要它用于私有方法。

4

0 回答 0