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 mock private property (Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock) #1244

Open
3 tasks done
oSumAtrIX opened this issue Apr 25, 2024 · 1 comment

Comments

@oSumAtrIX
Copy link

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 (Kinda? There are similar issues but none specifically for this one)

Expected Behavior

The property speed is correctly mocked:

  class Team {
      private var speed = 0
  }

  val mock = spyk(Team(), recordPrivateCalls = true)
  every { mock getProperty "speed" } returns 33

Current Behavior

An exception is thrown.

Steps to Reproduce

Merely this code example similar to the one from the docs fails:

  class Team {
      private var speed = 0
  }

  val mock = spyk(Team(), recordPrivateCalls = true)
  every { mock getProperty "speed" } returns 33

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version: 1.13.10
  • OS: Windows
  • Kotlin version: 1.9.22
  • JDK version: 11
  • Type of test: Unit test

Failure Logs

Please include any relevant log snippets or files here.

Stack trace

io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
	at io.mockk.impl.recording.states.StubbingState.checkMissingCalls(StubbingState.kt:14)
	at io.mockk.impl.recording.states.StubbingState.recordingDone(StubbingState.kt:8)
	at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
	at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:63)
	at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
	at io.mockk.MockKDsl.internalEvery(API.kt:94)
	at io.mockk.MockKKt.every(MockK.kt:143)

Minimal reproducible code (the gist of this issue)

package io.mockk.gh

import io.mockk.every
import io.mockk.mockk
import kotlin.test.Test

class IssueTest {
    @Test
    fun test() {
		  class Team {
		      private var speed = 0
		  }
		
		  val mock = spyk(Team(), recordPrivateCalls = true)
		  every { mock getProperty "speed" } returns 33
    }
}
@oSumAtrIX
Copy link
Author

Current workaround:

  private fun Any.setPrivateField(field: String, value: Any) {
        this::class.java.getDeclaredField(field).apply {
            this.isAccessible = true
            set(this@setPrivateField, value)
        }
    }

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