question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: StackOverflowError related to spyk'ing objects that call super method

See original GitHub issue

Prerequisites

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

*a similar, but possibly not quite exact error is here, people point to the JVM being the culprit which does not appear to be the issue here hence new issue

Expected Behavior

I am trying to spyk a class which calls a method in its super class. The class should then be mockable/verifiable.

Current Behavior

The tests fail to run with a java.lang.StackOverflowError.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. download repro project attached.
  2. run HomePresenterTest
  3. StackOverflowError

Context

  • MockK version: 1.12.0
  • OS: MacOS 11.3.1
  • Kotlin version: 1.5.21, 1.5.30
  • JDK version: 11.0.12 (x86_64) “Eclipse Temurin” - “Eclipse Temurin 11”. However also reproducible on following versions: 16.0.2 (x86_64) “Homebrew” - “OpenJDK 16.0.2” 11.0.10 (x86_64) “UNDEFINED” - “OpenJDK 11.0.10”
  • JUnit version: 4.13.2
  • Type of test: unit test
  • Robolectric version: 4.5.1

Stack trace

java.lang.StackOverflowError
	at kotlin.reflect.jvm.internal.impl.types.AbstractClassTypeConstructor.equals(AbstractClassTypeConstructor.java:76)
	at kotlin.jvm.internal.Intrinsics.areEqual(Intrinsics.java:167)
	at kotlin.reflect.jvm.internal.impl.types.checker.ClassicTypeSystemContext$DefaultImpls.isEqualTypeConstructors(ClassicTypeSystemContext.kt:216)
	at kotlin.reflect.jvm.internal.impl.types.checker.SimpleClassicTypeSystemContext.isEqualTypeConstructors(NewKotlinTypeChecker.kt:33)
	at kotlin.reflect.jvm.internal.impl.types.AbstractStrictEqualityTypeChecker.strictEqualSimpleTypes(AbstractStrictEqualityTypeChecker.kt:44)
	at kotlin.reflect.jvm.internal.impl.types.AbstractStrictEqualityTypeChecker.strictEqualTypesInternal(AbstractStrictEqualityTypeChecker.kt:29)
	at kotlin.reflect.jvm.internal.impl.types.AbstractStrictEqualityTypeChecker.strictEqualTypes(AbstractStrictEqualityTypeChecker.kt:13)
	at kotlin.reflect.jvm.internal.impl.types.checker.StrictEqualityTypeChecker.strictEqualTypes(NewKotlinTypeChecker.kt:45)
	at kotlin.reflect.jvm.internal.impl.types.KotlinType.equals(KotlinType.kt:117)
	at kotlin.jvm.internal.Intrinsics.areEqual(Intrinsics.java:167)
	at kotlin.reflect.jvm.internal.impl.renderer.DescriptorRendererImpl.renderSimpleType(DescriptorRendererImpl.kt:161)
	at kotlin.reflect.jvm.internal.impl.renderer.DescriptorRendererImpl.renderNormalizedTypeAsIs(DescriptorRendererImpl.kt:156)
	at kotlin.reflect.jvm.internal.impl.renderer.DescriptorRendererImpl.renderNormalizedType(DescriptorRendererImpl.kt:134)
	at kotlin.reflect.jvm.internal.impl.renderer.DescriptorRendererImpl.renderType(DescriptorRendererImpl.kt:116)
	at kotlin.reflect.jvm.internal.ReflectionObjectRenderer.renderType(ReflectionObjectRenderer.kt:122)
	at kotlin.reflect.jvm.internal.KTypeImpl.toString(KTypeImpl.kt:125)
	at io.mockk.impl.instantiation.JvmMockFactoryHelper.toDescription$mockk(JvmMockFactoryHelper.kt:129)
	at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:25)
	at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:21)
	at com.example.mockkstackoverflowexceptiondemo.base.HomePresenter.onViewAttached(Base.kt:40)
        at com.example.mockkstackoverflowexceptiondemo.base.HomePresenter.onViewAttached(Base.kt:33)
... snip ...

Minimal reproducible code (the gist of this issue)

Project code

package com.example.mockkstackoverflowexceptiondemo.base

import androidx.annotation.CallSuper


abstract class MvpPresenter<V : MvpView> {
    var view: V? = null

    fun attachView(view: V) {
        this.view = view
        onViewAttached(view)
    }

    fun detachView() { view = null }

    @CallSuper
    open fun onViewAttached(view: V) {
        // not reproducible if abstract.
    }
}

interface MvpView

class HomePresenter : MvpPresenter<HomeView>() {

    override fun onViewAttached(view: HomeView) {
        super.onViewAttached(view)
    }
}

interface HomeView : MvpView

Test code

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28])
class HomePresenterTest {

    @Test fun someTest() {
        val presenter = spyk(HomePresenter())
        val view = mockk<HomeView>(relaxed = true)
        presenter.attachView(view)
    }
}

mockkstackoverflowexceptiondemo.zip

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

1reaction
TomBell-Trovecommented, Apr 16, 2022

This is still an issue and should not be closed without investigation. I’ve attached a sample above to repro.

On Sat, 16 Apr 2022, 7:58 pm stale[bot], @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/mockk/mockk/issues/704#issuecomment-1100599963, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVBUZKLP6QCIEJS2JDNYOMTVFJXJDANCNFSM5E4I6G2Q . You are receiving this because you authored the thread.Message ID: @.***>

1reaction
Michael-MFcommented, Dec 9, 2021

I’m having the same issue, no Roboeletric involved for me though, just a simple JUnit4 test

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - StackOverflowError exception when calling method
I am getting StackOverflowError overflow exception when calling a method that only contains print statement. Below is the code public class Dog ...
Read more >
The StackOverflowError in Java
When a method is called, a new stack frame gets created on the call stack. This stack frame holds parameters of the invoked...
Read more >
Bug listing with status UNCONFIRMED as at 2022/12/19 13 ...
Bug :128538 - "sys-apps/coreutils: /bin/hostname should be installed from coreutils not sys-apps/net-tools" status:UNCONFIRMED resolution: severity:enhancement ...
Read more >
The only 3 steps you need to mock an API call in Jest
You can incrementally add some of the concepts below to super-charge your mocks: Check out the other mock function methods listed in the...
Read more >
Spring Boot Reference Documentation
If you are starting out with Spring, try one of the guides. Ask a question. We monitor stackoverflow.com for questions tagged with spring-boot...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found