8000 Bug: class redefinition failed: attempted to add a method · Issue #477 · mockk/mockk · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Bug: class redefinition failed: attempted to add a method #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nezed opened this issue Aug 25, 2020 · 3 comments
Closed

Bug: class redefinition failed: attempted to add a method #477

nezed opened this issue Aug 25, 2020 · 3 comments
Labels

Comments

@nezed
Copy link
nezed commented Aug 25, 2020

Hi, guys!
I'm just trying to mock class which defined in external package and looks like it's impossible for now due package-private visibility.
Here you can view source code of class which i'm trying to mock.

Expected Behavior

Classes that comes in packages must be mockable

Current Behavior

Unable to mock class with package-private visibility

Failure Information (for bugs)

Steps to Reproduce

// build.gradle.kts
dependencies {
  implementation(group = "ru.yandex.clickhouse", name = "clickhouse-jdbc", version = "0.2.4")

  testImplementation(group = "io.mockk", name = "mockk", version = "1.10.0")
}

Case 1: Instance mock

import ru.yandex.clickhouse.Writer

fun main(args: Array<String>) {
  val writerMock = mockk<Writer>()
}

Case 2: Class mock

import ru.yandex.clickhouse.Writer

fun main(args: Array<String>) {
  val writerMock = mockkClass(Writer::class)
}

Case 3: Constructor mock

import ru.yandex.clickhouse.Writer

fun main(args: Array<String>) {
  mockkConstructor(Writer::class)
  every { anyConstructed<Writer>().send() } just Runs
}

Case 4: Static mock

import ru.yandex.clickhouse.Writer

fun main(args: Array<String>) {
  mockkStatic("ru.yandex.clickhouse.Writer")
}

All four cases described above leading to same result:

DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Writer name=#1
WARN io.mockk.proxy.jvm.transformation.JvmInlineInstrumentation - Failed to transform classes [class ru.yandex.clickhouse.Writer, class ru.yandex.clickhouse.ConfigurableApi, class java.lang.Object]
java.lang.UnsupportedOperationException: class redefinition failed: attempted to add a method
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:167)

Case 5: Referencing class

import ru.yandex.clickhouse.Writer

fun main(args: Array<String>) {
  mockkStatic("ru.yandex.clickhouse.Writer")
  every { Writer(any()).send() } just Runs
}

results:

e: /Users/.../ClickhouseClientTests.kt: (67, 13): Cannot access '<init>': it is package-private in 'Writer'
FAILURE: Build failed with an exception.

Context

  • MockK version: 1.10.0
  • OS: OS X 10.14.6
  • Kotlin version: 1.3.50-release-112 (JRE 11.0.6+10)
  • JDK version: 11.0.6+10
  • JUnit version: kotest-runner-junit5-jvm@4.0.6
  • Type of test: unit test

Failure Logs

Please include any relevant log snippets or files here.

Stack trace

expand
DEBUG io.mockk.impl.instantiation.JvmStaticMockFactory - Creating static mockk for Writer
DEBUG io.mockk.proxy.jvm.StaticProxyMaker - Transforming class ru.yandex.clickhouse.Writer for static method interception
WARN io.mockk.proxy.jvm.transformation.JvmInlineInstrumentation - Failed to transform classes [class ru.yandex.clickhouse.Writer]
java.lang.UnsupportedOperationException: class redefinition failed: attempted to add a method
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:167)
	at io.mockk.proxy.jvm.transformation.JvmInlineInstrumentation.retransform(JvmInlineInstrumentation.kt:28)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$execute$1.invoke(RetransformInlineInstrumnetation.kt:19)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$execute$1.invoke(RetransformInlineInstrumnetation.kt:6)
	at io.mockk.proxy.common.transformation.ClassTransformationSpecMap.applyTransformation(ClassTransformationSpecMap.kt:41)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation.execute(RetransformInlineInstrumnetation.kt:16)
	at io.mockk.proxy.jvm.StaticProxyMaker.staticProxy(StaticProxyMaker.kt:33)
	at io.mockk.impl.instantiation.JvmStaticMockFactory.staticMockk(JvmStaticMockFactory.kt:39)
	at ...ClickhouseClientTests$1.invoke(ClickhouseClientTests.kt:223)
	at ...ClickhouseClientTests$1.invoke(ClickhouseClientTests.kt:31)
	at io.kotest.core.spec.style.FreeSpec.<init>(freeSpec.kt:35)
	at ...ClickhouseClientTests.<init>(ClickhouseClientTests.kt:31)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at io.kotest.core.engine.InstantiateSpecKt.javaReflectNewInstance(instantiateSpec.kt:21)
	at io.kotest.core.engine.InstantiateSpecKt.instantiateSpec(instantiateSpec.kt:15)
	at io.kotest.core.engine.SpecExecutor.createInstance(SpecExecutor.kt:88)
	at io.kotest.core.engine.SpecExecutor.execute(SpecExecutor.kt:35)
	at io.kotest.core.engine.KotestEngine$submitBatch$$inlined$forEach$lambda$1$1.invokeSuspend(KotestEngine.kt:66)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:84)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at io.kotest.core.engine.KotestEngine$submitBatch$$inlined$forEach$lambda$1.run(KotestEngine.kt:65)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at io.kotest.core.engine.InstantiateSpecKt.javaReflectNewInstance(instantiateSpec.kt:21)
	at io.kotest.core.engine.InstantiateSpecKt.instantiateSpec(instantiateSpec.kt:15)
	at io.kotest.core.engine.SpecExecutor.createInstance(SpecExecutor.kt:88)
	at io.kotest.core.engine.SpecExecutor.execute(SpecExecutor.kt:35)
	at io.kotest.core.engine.KotestEngine$submitBatch$$inlined$forEach$lambda$1$1.invokeSuspend(KotestEngine.kt:66)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:84)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at io.kotest.core.engine.KotestEngine$submitBatch$$inlined$forEach$lambda$1.run(KotestEngine.kt:65)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.UnsupportedOperationException: class redefinition failed: attempted to add a method
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:167)
	at io.mockk.proxy.jvm.transformation.JvmInlineInstrumentation.retransform(JvmInlineInstrumentation.kt:28)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$doCancel$1.invoke(RetransformInlineInstrumnetation.kt:38)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$doCancel$1.invoke(RetransformInlineInstrumnetation.kt:6)
	at io.mockk.proxy.common.transformation.ClassTransformationSpecMap.applyTransformation(ClassTransformationSpecMap.kt:41)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation.doCancel(RetransformInlineInstrumnetation.kt:32)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation.access$doCancel(RetransformInlineInstrumnetation.kt:6)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$execute$1$1.invoke(RetransformInlineInstrumnetation.kt:17)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$execute$1$1.invoke(RetransformInlineInstrumnetation.kt:6)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation.execute(RetransformInlineInstrumnetation.kt:23)
	at io.mockk.proxy.jvm.StaticProxyMaker.staticProxy(StaticProxyMaker.kt:33)
	at io.mockk.impl.instantiation.JvmStaticMockFactory.staticMockk(JvmStaticMockFactory.kt:39)
	at ...ClickhouseClientTests$1.invoke(ClickhouseClientTests.kt:223)
	at ...ClickhouseClientTests$1.invoke(ClickhouseClientTests.kt:31)
	at io.kotest.core.spec.style.FreeSpec.<init>(freeSpec.kt:35)
	at ...ClickhouseClientTests.<init>(ClickhouseClientTests.kt:31)
	... 23 more
java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at io.kotest.core.engine.InstantiateSpecKt.javaReflectNewInstance(instantiateSpec.kt:21)
	at io.kotest.core.engine.InstantiateSpecKt.instantiateSpec(instantiateSpec.kt:15)
	at io.kotest.core.engine.SpecExecutor.createInstance(SpecExecutor.kt:88)
	at io.kotest.core.engine.SpecExecutor.execute(SpecExecutor.kt:35)
	at io.kotest.core.engine.KotestEngine$submitBatch$$inlined$forEach$lambda$1$1.invokeSuspend(KotestEngine.kt:66)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:84)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at io.kotest.core.engine.KotestEngine$submitBatch$$inlined$forEach$lambda$1.run(KotestEngine.kt:65)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.UnsupportedOperationException: class redefinition failed: attempted to add a method
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:167)
	at io.mockk.proxy.jvm.transformation.JvmInlineInstrumentation.retransform(JvmInlineInstrumentation.kt:28)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$doCancel$1.invoke(RetransformInlineInstrumnetation.kt:38)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$doCancel$1.invoke(RetransformInlineInstrumnetation.kt:6)
	at io.mockk.proxy.common.transformation.ClassTransformationSpecMap.applyTransformation(ClassTransformationSpecMap.kt:41)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation.doCancel(RetransformInlineInstrumnetation.kt:32)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation.access$doCancel(RetransformInlineInstrumnetation.kt:6)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$execute$1$1.invoke(RetransformInlineInstrumnetation.kt:17)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$execute$1$1.invoke(RetransformInlineInstrumnetation.kt:6)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation.execute(RetransformInlineInstrumnetation.kt:23)
	at io.mockk.proxy.jvm.StaticProxyMaker.staticProxy(StaticProxyMaker.kt:33)
	at io.mockk.impl.instantiation.JvmStaticMockFactory.staticMockk(JvmStaticMockFactory.kt:39)
	at ...ClickhouseClientTests$1.invoke(ClickhouseClientTests.kt:223)
	at ...ClickhouseClientTests$1.invoke(ClickhouseClientTests.kt:31)
	at io.kotest.core.spec.style.FreeSpec.<init>(freeSpec.kt:35)
	at ...ClickhouseClientTests.<init>(ClickhouseClientTests.kt:31)
	... 23 more

java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at io.kotest.core.engine.InstantiateSpecKt.javaReflectNewInstance(instantiateSpec.kt:21)
	at io.kotest.core.engine.InstantiateSpecKt.instantiateSpec(instantiateSpec.kt:15)
	at io.kotest.core.engine.SpecExecutor.createInstance(SpecExecutor.kt:88)
	at io.kotest.core.engine.SpecExecutor.execute(SpecExecutor.kt:35)
	at io.kotest.core.engine.KotestEngine$submitBatch$$inlined$forEach$lambda$1$1.invokeSuspend(KotestEngine.kt:66)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:84)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at io.kotest.core.engine.KotestEngine$submitBatch$$inlined$forEach$lambda$1.run(KotestEngine.kt:65)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.UnsupportedOperationException: class redefinition failed: attempted to add a method
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:167)
	at io.mockk.proxy.jvm.transformation.JvmInlineInstrumentation.retransform(JvmInlineInstrumentation.kt:28)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$doCancel$1.invoke(RetransformInlineInstrumnetation.kt:38)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$doCancel$1.invoke(RetransformInlineInstrumnetation.kt:6)
	at io.mockk.proxy.common.transformation.ClassTransformationSpecMap.applyTransformation(ClassTransformationSpecMap.kt:41)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation.doCancel(RetransformInlineInstrumnetation.kt:32)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation.access$doCancel(RetransformInlineInstrumnetation.kt:6)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$execute$1$1.invoke(RetransformInlineInstrumnetation.kt:17)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$execute$1$1.invoke(RetransformInlineInstrumnetation.kt:6)
	at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation.execute(RetransformInlineInstrumnetation.kt:23)
	at io.mockk.proxy.jvm.StaticProxyMaker.staticProxy(StaticProxyMaker.kt:33)
	at io.mockk.impl.instantiation.JvmStaticMockFactory.staticMockk(JvmStaticMockFactory.kt:39)
	at ...ClickhouseClientTests$1.invoke(ClickhouseClientTests.kt:223)
	at ...ClickhouseClientTests$1.invoke(ClickhouseClientTests.kt:31)
	at io.kotest.core.spec.style.FreeSpec.<init>(freeSpec.kt:35)
	at ...ClickhouseClientTests.<init>(ClickhouseClientTests.kt:31)
	... 23 more

This issue may be related to #119 and #255

@stale
Copy link
stale bot commented Oct 25, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as stale just ask to put an important label.

@stale stale bot added the stale label Oct 25, 2020
@stale stale bot closed this as completed Nov 1, 2020
@dawidhyzy-ifolor
Copy link

I face the same issue when trying to mock activity annotated with @AndroidEntryPoint. @nezed did you find a solution?

@nezed
Copy link
Author
nezed commented Feb 24, 2021

Hi @dawidhyzy-ifolor!
We just mocking some of our upper-lever interfaces (facades/adapters/decorators) as workaround

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants
0