io.mockk.MockKException: Missing calls inside every { ... } block when mocking extension method

See original GitHub issue

Just be aware about usual MockK support pattern. Tickets are checked from time to time, replied, discussed, labeled, e.t.c. But real fixes are applied in a month-two month period in a bunch. If you think this is unacceptable, go on, join the project, change the world.

Please remove sections wisely

Below information is actually needed to make all the process of fixing faster. Choose main points. Don’t blindly follow this as a set of rules. Don’t waste much time. Usually, the main thing is to have a good reproducible minimal code.

Prerequisites

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

  • [YES ] I am running the latest version
  • [ YES] I checked the documentation and found no answer
  • [ YES] I checked to make sure that this issue has not already been filed

Expected Behavior

Test should not fail with io.mockk.MockKException: Missing calls inside every { ... } block. exception. Extension method should be mocked.

Current Behavior

When trying to mock one of the extensions defined in kotlin.io.IOSteams.kt - test fails with io.mockk.MockKException: Missing calls inside every { ... } block. exception.

Failure Information (for bugs)

Kotlin version: 1.3.41 mockk version: 1.9.3 junit version: 4.12

Steps to Reproduce

@RunWith(MockitoJUnitRunner::class)
class ExampleTest {
    val mockInputStream = mockk<InputStream>()
    val mockReader = mockk<BufferedReader>()
    
    @Test
    fun `this is just example test`() {
        mockkStatic("kotlin.io.ByteStreamsKt")
        every { mockInputStream.bufferedReader() } returns mockReader
    }
}

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.9.3
  • OS: MacOS Catalina
  • Kotlin version: 1.3.41
  • JDK version: 1.8
  • JUnit version: 4.12
  • Type of test: unit test

Failure Logs

Please include any relevant log snippets or files here.

Stack trace

io.mockk.MockKException: Missing calls inside every { ... } block.

	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:60)
	at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
	at io.mockk.MockKDsl.internalEvery(API.kt:92)
	at io.mockk.MockKKt.every(MockK.kt:104)
	at com.example.ExampleTest.this is just example test(ExampleTest.kt:26)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
	at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
	at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
	at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
antailyaqwercommented, Jan 8, 2022

+1 to a comment higher every {} not working with spyk objects. every {spykObject getProperty "field} answers {1}

3reactions
iainismcommented, Feb 12, 2020

I think I hit this as well - I worked around it with some significant refactoring. It would have been nice not to have to do so though! (Please mark as important)

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - Mockk Missing calls inside every { ... } block
Run the Instrumented Tests in an emulator >= Android-P; Set Foo class as open (and the method(s) you want to mock too).
Read more >
mockk-io/Lobby - Gitter
Hello I am new to using Mockk. I am trying to mock an object however I keep getting the following error io.mockk.MockKException: Missing...
Read more >
Android : Mockk Missing calls inside every { ... } block - YouTube
Android : Mockk Missing calls inside every { ... } block [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] Android ...
Read more >
Mock singleton objects and static methods | MockK Guidebook
This approach works with any Kotlin object, which includes companion object s and enum class elements. Mocking static methods #. Sometimes you may...
Read more >
[Solved]-Kotlin Mockk Error: Missing calls inside verify ...
database should be mock or a spy to call verify on it. I assume that it's not a mock because you are calling...
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