Mocking any of the Kotlin functional interfaces with Roboletric runner used results in a ClassCastException

171 views
Skip to first unread message

Matej Drobnič

unread,
Jan 18, 2018, 5:23:08 AM1/18/18
to mockito
Running this code:

@RunWith(RobolectricTestRunner.class)
public class MockitoTest {
    @Test
    public void testFunctionMock() throws InterruptedException {
        Mockito.mock(kotlin.Function.class);
    }
}

With following dependencies:

dependencies {
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:1.2.20"
    testImplementation 'junit:junit:4.12'
    testImplementation "org.mockito:mockito-core:2.13.0"
    testImplementation "org.robolectric:robolectric:3.6.1"
}

results in ClassCastException:

ClassCastException occurred while creating the mockito mock :
  class to mock : 'kotlin.Function', loaded by classloader : 'sun.misc.Launcher$AppClassLoader@15db9742'
  created class : 'kotlin.Function$MockitoMock$888821150', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@dc79225'
  proxy instance class : 'kotlin.Function$MockitoMock$888821150', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@dc79225'
  instance creation by : ObjenesisInstantiator

You might experience classloading issues, please ask the mockito mailing-list.


at com.matejdro.mockitotest.MockitoTest.testFunctionMock(MockitoTest.java:12)
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.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:535)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:249)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:123)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
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.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:77)
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.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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 com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: java.lang.ClassCastException: kotlin.Function$MockitoMock$888821150 cannot be cast to org.mockito.internal.creation.bytebuddy.MockAccess
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:48)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:25)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:65)
at org.mockito.Mockito.mock(Mockito.java:1821)
at org.mockito.Mockito.mock(Mockito.java:1734)
... 30 more

Apparently something weird is going on with kotlin and roboletric class loaders. So far I've been able to figure out the fact that MockitoMock object is loaded by different ClassLoader than MockAccess object, so that might be causing issues?

Removing RunWith method (so it runs with normal JUnit test runner) fixes the issue, but that is of course not proper fix (I want to use roboletric).

Any idea what could cause this? This happens with all kotlin functional interfaces (Function, Function1, Function2 etc.)

Cody Dunlap

unread,
Sep 12, 2018, 2:51:45 PM9/12/18
to mockito
Did you ever find a fix for this? I'm running into this now and can't seem to find a solid solution.
Reply all
Reply to author
Forward
0 new messages