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.

Can not mock super method call in mockK?

See original GitHub issue

Prerequisites

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

  • 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

Expected Behavior

super.print() is mocked by answers.

Current Behavior

super.print() is not mocked.

Context

  • MockK version: 1.11.0
  • OS: Mac OsX
  • Kotlin version: 1.3
  • JDK version: Java 8
  • JUnit version: 4.13.1
  • Type of test: unit test

Minimal reproducible code (the gist of this issue)

class SuperCallTest {

    open class Parent {
        open fun print() {
            println("Parent")
        }
    }

    class Children: Parent() {
        override fun print() {
            super.print()
            println("Children")
        }
    }

    @Test
    fun testCallSuper() {
        val children = Children()
        val spy = spyk(children)

        every { (spy as Parent).print() } answers { nothing }

        children.print()
    }

    @Test
    fun testCallSuper2() {
        val children = Children()
        val spy = spyk(children as Parent)

        every { spy.print() } answers { nothing }

        children.print()
    }

    @Test
    fun testCallSuper3() {
        mockkConstructor(Parent::class)
        every { anyConstructed<Parent>().print() } answers { nothing }

        val children = Children()

        children.print()
    }
}

Output

Console output:

Parent
Children

for both 3 cases.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

5reactions
ChandoraAnkitcommented, Dec 5, 2021

Any update on this?. I’m also facing the same issue for both the answers and returns

1reaction
stale[bot]commented, Jun 26, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to mock super method by MockK - Stack Overflow
I want to use MockK to verify if onCreate() is invoked, then doSomething() is invoked too. However I don't know how to mock...
Read more >
MockK | mocking library for Kotlin
Provides DSL to mock behavior. Built from zero to fit Kotlin language. Supports named parameters, object mocks, coroutines and extension function mocking.
Read more >
How To Mock Super Class Method In Junit - Roy Tutorials
Here in this example I am going to show you how to mock super class method in Junit test class. A class that...
Read more >
mockk-io/Lobby - Gitter
Hi all, I am trying to mock a class that has no default constructor and that it is provided as a dependecy to...
Read more >
Mock constructors in code you don't own | MockK Guidebook
Rather than building a new instance every time the constructor is called, MockK generates a singleton and always returns the same instance. This...
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