Skip to content

At Android OS, mockkConstructor does not work when the class contains a companion object #1284

@LitterSun

Description

@LitterSun

At Android OS, mockkConstructor does not work when the class contains a companion object

Context

  • MockK version: master branch
  • OS: Android

Stack trace

08-12 15:59:42.468 32717 32736 W kk.android.test: FAILURE TO RETRANSFORM Unable to perform redefinition of 'Lio/mockk/XxxTest$MockCls;': Failed to verify class. Error was: Verifier rejected class io.mockk.XxxTest$MockCls: void io.mockk.XxxTest$MockCls.<clinit>(): [0xFFFFFFFF] register index out of range (7 >= 7)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: Failed to transform classes [class io.mockk.XxxTest$MockCls]
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: java.lang.RuntimeException: Could not retransform classes: 62
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.proxy.android.JvmtiAgent.nativeRetransformClasses(Native Method)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.proxy.android.JvmtiAgent.requestTransformClasses(JvmtiAgent.kt:66)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.proxy.android.transformation.AndroidInlineInstrumentation.retransform(AndroidInlineInstrumentation.kt:19)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.proxy.common.transformation.RetransformInlineInstrumentation$execute$1.invoke(RetransformInlineInstrumentation.kt:19)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.proxy.common.transformation.RetransformInlineInstrumentation$execute$1.invoke(RetransformInlineInstrumentation.kt:16)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.proxy.common.transformation.ClassTransformationSpecMap.applyTransformation(ClassTransformationSpecMap.kt:41)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.proxy.common.transformation.RetransformInlineInstrumentation.execute(RetransformInlineInstrumentation.kt:16)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.proxy.android.ConstructorProxyMaker.constructorProxy(ConstructorProxyMaker.kt:19)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.impl.instantiation.JvmConstructorMockFactory$ConstructorInvocationHandler.push(JvmConstructorMockFactory.kt:205)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.impl.instantiation.JvmConstructorMockFactory.constructorMockk(JvmConstructorMockFactory.kt:261)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at io.mockk.XxxTest.testMockkConstructor(XxxTest.kt:72)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at java.lang.reflect.Method.invoke(Native Method)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:162)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.Suite.runChild(Suite.java:128)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.Suite.runChild(Suite.java:27)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:67)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:446)
08-12 15:59:42.468 32717 32736 W idInlineInstrumentation: 	at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2322)

Minimal reproducible code (the gist of this issue)

    class MockCls {
        fun add(a: Int, b: Int) = a + b

        companion object {
            const val VALUE = 5
        }
    }

    @Test
    fun testMockkConstructor() {
        mockkConstructor(MockCls::class)
        every { anyConstructed<MockCls>().add(1, 2) } returns 4
        assertEquals(4, MockCls().add(1, 2))
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions