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

getProperty can't find private property in parent class #1070

Open
3 tasks done
ShinJJang opened this issue Mar 22, 2023 · 1 comment
Open
3 tasks done

getProperty can't find private property in parent class #1070

ShinJJang opened this issue Mar 22, 2023 · 1 comment

Comments

@ShinJJang
Copy link

ShinJJang commented Mar 22, 2023

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

getProperty is work with private field of parent class.

Current Behavior

Can't find property signature in mock class(child class).

Failure Information (for bugs)

Bug 1 (Normal case, property is not accessable in child)
InternalPlatformDsl#dymaicGet in JVM find only Mock Class. dymaicGet need to find there parent class.

For reference above issue, I try to apply allAncestorProperties to dymaicGet, but occur below error.

// added
private fun KClass<*>.allAncestorProperties(): Sequence<KProperty<*>> {
        return (sequenceOf(this) + this.allSuperclasses.asSequence())
            .flatMap { it.memberProperties }
    }

// changed: memberProperties to allAncestorProperties 
actual fun dynamicGet(self: Any, name: String): Any? {
        val property = self::class.allAncestorProperties()
            .filterIsInstance<KProperty1<Any, Any?>>()
        ...
}

io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock. (except shadowed test)

I have could not resolve this.

Bug 2 (Shadowed property of private property in parent class as default accessor)
I don't know what is root cause and this is related with Bug 1

Context

  • MockK version: latest (master #6587d9bc616111eb3423aa1b9ba4c27f1e3addbf)
  • OS: MacOS 12.5.1
  • Kotlin version: 1.8
  • JDK version: 11
  • JUnit version:
  • Type of test: Kotlin test

Failure Logs

io.mockk.MockKException: can't find property privateProperty for dynamic property get

Stack trace

// -----------------------[ YOUR STACK STARTS HERE ] -----------------------
can't find property privateProperty for dynamic property get
io.mockk.MockKException: can't find property privateProperty for dynamic property get
	at app//io.mockk.InternalPlatformDsl.dynamicGet(InternalPlatformDsl.kt:164)
	at app//io.mockk.MockKMatcherScope.getProperty(API.kt:2065)
	at app//io.mockk.it.PrivateParentPropertyTest$testPrivatePropertyMock$1.invoke(PrivateParentPropertyTest.kt:45)
	at app//io.mockk.it.PrivateParentPropertyTest$testPrivatePropertyMock$1.invoke(PrivateParentPropertyTest.kt:45)
	at app//io.mockk.impl.eval.RecordedBlockEvaluator$record$block$1.invoke(RecordedBlockEvaluator.kt:25)
	at app//io.mockk.impl.eval.RecordedBlockEvaluator$enhanceWithRethrow$1.invoke(RecordedBlockEvaluator.kt:78)
	at app//io.mockk.impl.recording.JvmAutoHinter.autoHint(JvmAutoHinter.kt:23)
	at app//io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:40)
	at app//io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
	at app//io.mockk.MockKDsl.internalEvery(API.kt:94)
	at app//io.mockk.MockKKt.every(MockK.kt:143)
	at app//io.mockk.it.PrivateParentPropertyTest.testPrivatePropertyMock(PrivateParentPropertyTest.kt:45)
	at java.base@11.0.16/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base@11.0.16/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base@11.0.16/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base@11.0.16/java.lang.reflect.Method.invoke(Method.java:566)
	at app//org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
	at app//org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
	...
// -----------------------[ YOUR STACK TRACE ENDS HERE ] -----------------------

Minimal reproducible code (the gist of this issue)

// -----------------------[ GRADLE DEFINITIONS ] -----------------------
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
package io.mockk.it

import io.mockk.every
import io.mockk.spyk
import io.mockk.verify
import kotlin.test.Test
import kotlin.test.assertEquals

class PrivateParentPropertyTest {
    open class Parent {
        private val privateProperty : String = "Real property"

        open fun call(): String {
            return privateProperty
        }
    }

    open class Child: Parent()

    class ChildWithShadowedProperty: Parent() {
        val privateProperty : String = "Shadowed property"

        override fun call(): String {
            return privateProperty
        }
    }

    class GrandChild: Child()

    @Test
    fun testChildShadowedProperty() {
        val mock = spyk(ChildWithShadowedProperty(), recordPrivateCalls = true)
        every { mock getProperty "privateProperty" } returns "Mock"

        assertEquals(mock.call(), "Mock")
    }

    @Test
    fun testPrivatePropertyMock() {
        val mock = spyk(Child(), recordPrivateCalls = true)
        every { mock getProperty "privateProperty" } returns "Mock"

        assertEquals(mock.call(), "Mock")
    }

    @Test
    fun testPrivatePropertyMockForGrandChild() {
        val mock = spyk(GrandChild(), recordPrivateCalls = true)
        every { mock getProperty "privateProperty" } returns "Mock"

        assertEquals(mock.call(), "Mock")
    }

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

        mock.call()

        verify { mock getProperty "privateProperty" }
    }
}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------
@Carlos-Ot
Copy link

Carlos-Ot commented Apr 20, 2023

I would like to have this feature too. I'm struggling with this for a while now. 😢

@ShinJJang I think your solution will work, try to change your private property to a lazy {} property and have it a go. There's another issue with mocking private instances when they are statically initialized: #263

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants