Skip to content
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

Bug: ClassCastException(Invalid name: byte[]) in generic interface mocking #300

Open
Makentoshe opened this issue May 14, 2019 · 2 comments

Comments

@Makentoshe
Copy link

Makentoshe commented May 14, 2019

Expected Behavior

Mocked method should returns ByteArray

Current Behavior

If we tries to mock the get method in the selected interface:

interface A<K, V> {
    fun get(k: K): V?
}

like this

val a = mockk<A<Any, ByteArray>>()
every { a.get(any()) } returns byteArrayOf()

the test will be crashed with an exception.

Steps to Reproduce

  1. Create the interface
interface A<K, V> {
    fun get(k: K): V?
}
  1. Run selected in a test case
val a = mockk<A<Any, ByteArray>>()
every { a.get(any()) } returns byteArrayOf()

Context

  • MockK version: 1.8.13.kotlin13
  • OS: Win10
  • Kotlin version: 1.3.30
  • JDK version: 1.8.0_144
  • JUnit version: JUnit 4.12
  • Type of test: android instrumented test

Also using androidx artifacts

Stack trace

Just a stack trace directly from my app

java.lang.ClassNotFoundException: Invalid name: byte[]
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:453)
at java.lang.Class.forName(Class.java:378)
at io.mockk.impl.recording.JvmAutoHinter.autoHint(JvmAutoHinter.kt:38)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:36)
at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:25)
at io.mockk.MockKDsl.internalEvery(API.kt:93)
at io.mockk.MockKKt.every(MockK.kt:104)
at com.makentoshe.booruchan.common.download.PreviewImageDownloadControllerATest.shouldReturnSuccessForFirst(PreviewImageDownloadControllerATest.kt:36)
at java.lang.reflect.Method.invoke(Native Method)
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 androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
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.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
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 org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:388)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2089)

Minimal reproducible code (the gist of this issue)

dependencies {
    testImplementation "io.mockk:mockk:1.8.13.kotlin13"
    androidTestImplementation "io.mockk:mockk:1.8.13.kotlin13"
    androidTestImplementation "io.mockk:mockk-android:1.8.13.kotlin13"
 }

import io.mockk.every
import io.mockk.mockk
import org.junit.Test

class InterfaceBugTest {

    interface BugInterface<K, V> {
        fun get(k: K): V?
    }

    @Test
    fun causeBug() {
        val bugInterface = mockk<BugInterface<Any, ByteArray>>()
        every { bugInterface.get(any()) } returns byteArrayOf()
    }
}
@oleksiyp
Copy link
Collaborator

Nice one

@stale
Copy link

stale bot commented Jul 23, 2019

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 put an important tag.

@stale stale bot added the stale label Jul 23, 2019
@stale stale bot closed this as completed Jul 30, 2019
@oleksiyp oleksiyp changed the title Bug: Interface with 2 generics mock (Invalid name: byte[]) Bug: ClassCastException(Invalid name: byte[]) in generic interface mocking Nov 2, 2019
@oleksiyp oleksiyp reopened this Nov 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Critical to fix
  
Awaiting triage
Development

No branches or pull requests

2 participants