为什么我得到java.lang.UnsupportedOperationException:类重新定义失败:尝试添加方法

时间:2019-07-10 20:45:07

标签: android unit-testing kotlin

下午好。 我正在与Kotlin中的Mockk编写单元测试。 我需要模拟Android类'SpannableString'

这是我的代码

private val spannableString = mockk<SpannableString>()
    @Before
    open fun setup() {
    every { spannableString.toString() } returns SPANNABLE_STRING_VALUE
    every { spannableStringWrapper.get() } returns spannableString
    every { suggestion.id } returns SUGGESTION_ID
    every { suggestion.label } returns "The Shard"
    every { customerSessionStorageProvider.guid } returns "guid"
}

因此,当我运行测试类时,我会得到:

java.lang.UnsupportedOperationException: class redefinition failed: attempted to add a method

at sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
at sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:144)
at io.mockk.proxy.jvm.transformation.InlineInstrumentation.doCancel(InlineInstrumentation.kt:60)
at io.mockk.proxy.jvm.transformation.InlineInstrumentation.access$doCancel(InlineInstrumentation.kt:9)
at io.mockk.proxy.jvm.transformation.InlineInstrumentation$execute$cancellation$1.invoke(InlineInstrumentation.kt:20)
at io.mockk.proxy.jvm.transformation.InlineInstrumentation$execute$cancellation$1.invoke(InlineInstrumentation.kt:9)
at io.mockk.proxy.jvm.transformation.InlineInstrumentation.execute(InlineInstrumentation.kt:43)
at io.mockk.proxy.jvm.ProxyMaker.inline(ProxyMaker.kt:88)
at io.mockk.proxy.jvm.ProxyMaker.proxy(ProxyMaker.kt:30)
at io.mockk.impl.instantiation.JvmMockFactory.newProxy(JvmMockFactory.kt:34)
at io.mockk.impl.instantiation.AbstractMockFactory.newProxy$default(AbstractMockFactory.kt:29)
at io.mockk.impl.instantiation.AbstractMockFactory.mockk(AbstractMockFactory.kt:58)

任何人都知道为什么我会收到该错误?

预先感谢

0 个答案:

没有答案