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

can't find private function inside superclass for dynamic call #425

Closed
shaohui10086 opened this issue Apr 1, 2020 · 3 comments · Fixed by #785
Closed

can't find private function inside superclass for dynamic call #425

shaohui10086 opened this issue Apr 1, 2020 · 3 comments · Fixed by #785

Comments

@shaohui10086
Copy link

shaohui10086 commented Apr 1, 2020

Expected Behavior

should find the private function inside superclass

Current Behavior

can't find function callPrivate() for dynamic call

Context

  • MockK version: 1.9.3
  • OS: Mac OS
  • Kotlin version: 1.3.31
  • JDK version: 1.8

Failure Logs

io.mockk.MockKException: can't find function callPrivate() for dynamic call

Stack trace

// -----------------------[ YOUR STACK STARTS HERE ] -----------------------
	io.mockk.MockKException: can't find function callPrivate() for dynamic call

	at io.mockk.InternalPlatformDsl.dynamicCall(InternalPlatformDsl.kt:122)
	at io.mockk.MockKMatcherScope$DynamicCall.invoke(API.kt:1969)
// -----------------------[ YOUR STACK TRACE ENDS HERE ] -----------------------

Minimal reproducible code (the gist of this issue)

class TestPrivateCall {
  open class Parent {
        fun call() {
            callPrivate()
        }
        private fun callPrivate() {}
    }

    class Child: Parent() {}

    @Test
    fun testPrivateCall() {
        val mock = spyk(Child(), recordPrivateCalls = true)

        mock.call()

        verify { mock["callPrivate"]() }
    }
}
@junesolar
Copy link

meet the same problem,when can it be fixed?

@NWuensche
Copy link
Contributor

Furthermore, when I use confirmVerified(mock), it tells me that I have not verified Child(#1).callPrivate(), which is really annoying.

@NWuensche
Copy link
Contributor

By the way, one can still verify the call of callPrivate by doing the following:

class Test {
    open class Parent {
        fun call() {
            callPrivate()
        }
        private fun callPrivate() {}
    }

    class Child: Parent() {}

    @Test
    fun testPrivateCall() {
        val mock = spyk(Child(), recordPrivateCalls = true)

        mock.call()

        verify { mock.callPrivateFun("callPrivate") }
    }

    private fun <T : Any> T.callPrivateFun(funName: String) {
        this::class
            .superclasses
            .map { it.declaredMembers }
            .flatten()
            .first { it.name == funName }!!
            .let {
                it.isAccessible = true
                it.call(this)
            }
    }
}

npars added a commit to npars/mockk that referenced this issue Feb 2, 2022
- Add support for mocking non-accessible parent methods using Mockk
- The class being mocked is always searched first for the specified
  method, then all superclasses are checked in no specific order. If
  multiple superclasses have matching signatures for the method then
  there is no guarantee which will be mocked first.
- Fixes mockk#425
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants