Mockk: 1.10.3 not working with Kotlin 1.3.72

Created on 9 Dec 2020  路  3Comments  路  Source: mockk/mockk

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • [x] I am running the latest version
  • [x] I checked the documentation and found no answer
  • [x] I checked to make sure that this issue has not already been filed

Expected Behavior

Upgrading from 1.10.2 to 1.10.3 should not break existing code.

Current Behavior

Code does not execute anymore due to reflective use of Kotlin 1.4 API.

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. run the provided with version 1.10.2
  2. update to version 1.10.3
  3. run

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version: 1.10.3
  • OS: Linux
  • Kotlin version: 1.3.72
  • JDK version: 11.0.8
  • JUnit version: 5.7.0
  • Type of test: unit test

Stack trace

// -----------------------[ YOUR STACK STARTS HERE ] -----------------------
'void kotlin.jvm.internal.FunctionReferenceImpl.<init>(int, java.lang.Object, java.lang.Class, java.lang.String, java.lang.String, int)'
java.lang.NoSuchMethodError: 'void kotlin.jvm.internal.FunctionReferenceImpl.<init>(int, java.lang.Object, java.lang.Class, java.lang.String, java.lang.String, int)'
    at io.mockk.impl.JvmMockKGateway$mockTypeChecker$1.<init>(JvmMockKGateway.kt)
    at io.mockk.impl.JvmMockKGateway.<init>(JvmMockKGateway.kt:97)
    at io.mockk.impl.JvmMockKGateway.<clinit>(JvmMockKGateway.kt:172)
// -----------------------[ YOUR STACK TRACE ENDS HERE ] -----------------------

Minimal reproducible code (the gist of this issue)

// -----------------------[ GRADLE DEFINITIONS ] -----------------------

// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class MockkTest {
  @Test
  fun test() {
    val mockk = mockk<Mocked> { every { hi() } returns "Hello world" }

    assertEquals(mockk.hi(), "Hello world")
  }
}

class Mocked {
  fun hi() = "there"
}

// -----------------------[ YOUR CODE ENDS HERE ] -----------------------

Most helpful comment

I think I have found a fix for this, looks like the behavior for function references changed between kotlin 1.3 and 1.4, will include this in the next release which will hopefully support kotlin 1.3.* again.

All 3 comments

I have similar issue with:

implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.50"
testImplementation "io.mockk:mockk:1.10.3-jdk8"
class MockkTest {

    class Dependency1(val value1: Int)
    class Dependency2(val value2: String)

    class SystemUnderTest(
        val dependency1: Dependency1,
        val dependency2: Dependency2
    ) {
        fun calculate() =
            dependency1.value1 + dependency2.value2.toInt()
    }

    @Test
    fun calculateAddsValues() {
        val doc1 = mockk<Dependency1>()
        val doc2 = mockk<Dependency2>()

        every { doc1.value1 } returns 5
        every { doc2.value2 } returns "6"

        val sut = SystemUnderTest(doc1, doc2)

        assertEquals(11, sut.calculate())
    }
java.lang.NoSuchMethodError: kotlin.jvm.internal.FunctionReferenceImpl.<init>(ILjava/lang/Object;Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;I)V

    at io.mockk.impl.JvmMockKGateway$mockTypeChecker$1.<init>(JvmMockKGateway.kt)
    at io.mockk.impl.JvmMockKGateway.<init>(JvmMockKGateway.kt:97)
    at io.mockk.impl.JvmMockKGateway.<clinit>(JvmMockKGateway.kt:172)
    at jp.co.finc.fincapp.ui.billing.MockkTest.calculateAddsValues(MockkTest.kt:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

We're experiencing this with Kotlin 1.3.70 and MockK 1.10.3 on JVM for tests that worked with MockK 1.10.0

I think I have found a fix for this, looks like the behavior for function references changed between kotlin 1.3 and 1.4, will include this in the next release which will hopefully support kotlin 1.3.* again.

Was this page helpful?
0 / 5 - 0 ratings