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

StackOverflow when mocking a collaborator (interface) and answering with a subype of the declared collaborator's return-type. #517

Closed
chrisdenman opened this issue Oct 27, 2020 · 6 comments
Labels

Comments

@chrisdenman
Copy link

chrisdenman commented Oct 27, 2020

Please see the attached snippets or pull from mockk_stackOverflow.

  • MockK version: 1.10.2
  • OS: Linux Mint (also OSX)
  • Kotlin version: 1.4.10
  • JDKs: 8.0.265.j9-adpt, 11.0.9.j9-adpt, 12.0.2.j9-adpt, 13.0.2.j9-adpt, 14.0.2.j9-adpt. Targetting JDK8.
  • JUnit version: 5.7.0
  • Type of test: Unit
  • Gradle script in Groovy
  • It occurs in MockK 1.10.0
  • Note that the tests don't do 'anything', they just set-up an answer from a collaborator interface

Stack trace

Exception in thread "Test worker" java/lang/StackOverflowError
        at io/mockk/proxy/jvm/dispatcher/JvmMockKDispatcher.get (JvmMockKDispatcher.java:16)
        at java/lang/Object.hashCode (Object.java:119)
        at java/util/HashMap.hash (HashMap.java:339)
        at java/util/HashMap.get (HashMap.java:557)
        at sun/reflect/Reflection.filterMethods (Reflection.java:291)
        at java/lang/Class.getMethodHelper (Class.java:1265)

        ...       

        at java/lang/Object.hashCode (Object.java:119)
        at java/util/WeakHashMap.hash (WeakHashMap.java:298)
        at java/util/WeakHashMap.put (WeakHashMap.java:449)
        at java/util/zip/ZipFile.getInputStream (ZipFile.java:392)
        at java/util/jar/JarFile.getInputStream (JarFile.java:452)
        at sun/misc/URLClassPath$JarLoader$2.getInputStream (URLClassPath.java:1102)
        at sun/misc/Resource.cachedInputStream (Resource.java:100)
        at sun/misc/Resource.getByteBuffer (Resource.java:183)
        at java/net/URLClassLoader.defineClass (URLClassLoader.java:671)
        at java/net/URLClassLoader.access$400 (URLClassLoader.java:89)
        at java/net/URLClassLoader$ClassFinder.run (URLClassLoader.java:1086)
        at java/security/AccessController.doPrivileged (AccessController.java:770)
        at java/net/URLClassLoader.findClass (URLClassLoader.java:589)
        at java/lang/ClassLoader.loadClassHelper (ClassLoader.java:953)
        at java/lang/ClassLoader.loadClass (ClassLoader.java:898)
        at java/lang/ClassLoader.loadClass (ClassLoader.java:881)
        at org/gradle/internal/io/LineBufferingOutputStream.<init> (LineBufferingOutputStream.java:46)
        at org/gradle/internal/io/LineBufferingOutputStream.<init> (LineBufferingOutputStream.java:41)
        at org/gradle/internal/io/LineBufferingOutputStream.<init> (LineBufferingOutputStream.java:37)
        at org/gradle/internal/io/LinePerThreadBufferingOutputStream.getStream (LinePerThreadBufferingOutputStream.java:35)
        at org/gradle/internal/io/LinePerThreadBufferingOutputStream.print (LinePerThreadBufferingOutputStream.java:141)
        at java/lang/ThreadGroup.uncaughtException (ThreadGroup.java:872)
        at java/lang/ThreadGroup.uncaughtException (ThreadGroup.java:866)
        at java/lang/Thread.uncaughtException (Thread.java:1337)

Minimal reproducible code (the gist of this issue)

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id 'org.jetbrains.kotlin.jvm' version "1.4.10"
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

    testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0"
    testImplementation "io.mockk:mockk:1.10.2"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.0"
}

repositories {
    mavenCentral()
}

test {
    useJUnitPlatform()
}
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Test

interface Fails {
    fun fn(): Any
}

interface Passes {
    fun fn(): String
}

class StackOverflowSpec {

    @Test fun fails() {
        val collaborator = mockk<Fails>()
        every { collaborator.fn() } returns ""
    }

    @Test fun passes() {
        val collaborator = mockk<Passes>()
        every { collaborator.fn() } returns ""
    }
}
@aleksandar78
Copy link

We are having the same problem.
We are trying mockK with some test cases, for the first time.
The very strange thing is that "on my machine" test are executed without problems but "on my colleague machine" goes in overflow.
We had some JDK version differences (11.0.6 vs 11.0.9 of OpenJDK).
Also, we set Gradle execution JDK in Intellij Idea the same as a project JDK and suddenly test runs again.

That was very strange, especially when you try to start with a new mocking library in your code base.

I don't have other explanaition.

@chrisdenman
Copy link
Author

Thanks for the feedback. I'll see if I can reproduce your results. I shall also try running it from the command line, not from IDEA.

@vvalencia-cl
Copy link

Hi. I have the same problem, but I'm using just simple mocks.

This is an extract of the stacktrace:

Exception in thread "Test worker" java/lang/StackOverflowError
        at io/mockk/proxy/jvm/dispatcher/JvmMockKDispatcher.get (java.base@9/JvmMockKDispatcher.java:16)
        at java/lang/Object.hashCode (java.base@9/Object.java:122)
        at java/util/HashMap.hash (java.base@9/HashMap.java:339)
        at java/util/HashMap.get (java.base@9/HashMap.java:552)
        at jdk/internal/reflect/Reflection.filterMethods (java.base@9/Reflection.java:276)
        at java/lang/Class.getMethodHelper (java.base@9/Class.java:1457)
        at java/lang/Class.getMethod (java.base@9/Class.java:1361)
        at java/lang/Object.hashCode (java.base@9/Object.java:122)
        at java/util/HashMap.hash (java.base@9/HashMap.java:339)
        at java/util/HashMap.get (java.base@9/HashMap.java:552)
        at jdk/internal/reflect/Reflection.filterMethods (java.base@9/Reflection.java:276)
        at java/lang/Class.getMethodHelper (java.base@9/Class.java:1457)

I'm using this JDK: AdoptOpenJDK 11.0.9 OpenJ9

openjdk version "11.0.9" 2020-10-20
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9+11)
Eclipse OpenJ9 VM AdoptOpenJDK (build openj9-0.23.0, JRE 11 Linux amd64-64-Bit Compressed References 20201022_810 (JIT enabled, AOT enabled)

I've just discovered that the problem doesn't occur when we use JDK 11.0.8 or JDK 11.0.9 Hotspot

OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)

I hope that this could be of any help

@stale
Copy link

stale bot commented Mar 19, 2021

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 Mar 19, 2021
@gabfssilva
Copy link

+1

It seems this library only works with Hotspot.

@stale stale bot removed the stale label May 27, 2021
@stale
Copy link

stale bot commented Jul 30, 2021

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 Jul 30, 2021
@stale stale bot closed this as completed Aug 18, 2021
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

4 participants