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 access private property from Parent class #1082

Closed
3 tasks done
Carlos-Ot opened this issue Apr 19, 2023 · 0 comments
Closed
3 tasks done

Can't access private property from Parent class #1082

Carlos-Ot opened this issue Apr 19, 2023 · 0 comments

Comments

@Carlos-Ot
Copy link

Carlos-Ot commented Apr 19, 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

When spying on mock classes I want to have access to the private properties of the Parent class. And that those properties doesn't need to be lazy initialized in order to work as a mock.

Current Behavior

The test fails with the message can't find property hashMap for dynamic property get. Also, there's a problem with the private properties that are not mocked if they are static-initialized. They only work if they are lazy initialized. (Relates to #263)

Context

Relates to this Issue #425 which was fixed in this PR #785

I think the fix for the properties will be similar as the one for functions.

  • MockK version: 1.13.5
  • OS: Android 13
  • Kotlin version: 1.8.10
  • JDK version: 11
  • JUnit version: 4
  • Type of test: Unit Test

Stack trace

can't find property hashMap for dynamic property get
io.mockk.MockKException: can't find property hashMap for dynamic property get
	at app//io.mockk.InternalPlatformDsl.dynamicGet(InternalPlatformDsl.kt:159)
	at app//io.mockk.MockKMatcherScope.getProperty(API.kt:2065)
	at app//[app.package].MockkPrivatePropertyTest$should put a value in the map when the value doesn't exists in it$1.invoke(MockkPrivatePropertyTest.kt:17)
	at app//[app.package].MockkPrivatePropertyTest$should put a value in the map when the value doesn't exists in it$1.invoke(MockkPrivatePropertyTest.kt:17)
	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)

Minimal reproducible code (the gist of this issue)

class MockkPrivatePropertyTest {

    private val classUnderTest = spyk(ChildCls(), recordPrivateCalls = true)
    private val mockMap: MutableMap<String, String> = mockk(relaxed = true)

    @Test
    fun `should put a value in the map when the value doesn't exist in it`(){
        // Given
        every { classUnderTest getProperty "hashMap" } returns mockMap

        // When
        classUnderTest.checkAndUpdate("key", "value")

        // Then
        verify(exactly = 1) { mockMap[any()] }
        verify(exactly = 1) { mockMap[any()] = any() }
    }
}

abstract class ParentCls {
    private val hashMap: MutableMap<String, String> = mutableMapOf()

    fun checkAndUpdate(key: String, value: String) {
        if (hashMap[key] == null) {
            hashMap[key] = value
        }
    }
}

class ChildCls: ParentCls() {
    fun doSomethingElse() = Unit
}
@Carlos-Ot Carlos-Ot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 19, 2023
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

1 participant