question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock

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.

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

Expected Behavior

Please describe the behavior you are expecting Given I mock and object When I stub behavior on it Then I can use the mocked object.

Current Behavior

What is the current behavior? Given I mock and object When I stub behavior on it Then I receive an exception. io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock

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.12.3
  • OS: Mac 12.3.1
  • Kotlin version: 1.6.21
  • JDK version: 1.8
  • JUnit version: 4.13.2
  • Type of test: unit test OR android instrumented test: Unit

Stack trace

Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
	at app//io.mockk.impl.recording.states.StubbingState.checkMissingCalls(StubbingState.kt:14)
	at app//io.mockk.impl.recording.states.StubbingState.recordingDone(StubbingState.kt:8)
	at app//io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
	at app//io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:64)
	at app//io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
	at app//io.mockk.MockKDsl.internalEvery(API.kt:93)
	at app//io.mockk.MockKKt.every(MockK.kt:98)
	at app//com.example.myapplication.ExampleUnitTest.unableToStubBehavior(ExampleUnitTest.kt:14)
	at java.base@11.0.11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base@11.0.11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base@11.0.11/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base@11.0.11/java.lang.reflect.Method.invoke(Method.java:566)
	at app//org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at app//org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at app//org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at app//org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at app//org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at app//org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at app//org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at app//org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at app//org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at app//org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at app//org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at app//org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at app//org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at app//org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at app//org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at app//org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
	at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
	at java.base@11.0.11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base@11.0.11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base@11.0.11/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base@11.0.11/java.lang.reflect.Method.invoke(Method.java:566)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
	at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:176)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
	at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:133)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
	at app//worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
	at app//worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)

Minimal reproducible code (the gist of this issue)

// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
    testImplementation 'io.mockk:mockk:1.12.4'
    testImplementation 'junit:junit:4.13.2'
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
File: Engine.java
package com.example.myapplication;

public class Engine {
    final int cylinderCount = 6;
}

File: Car.java
package com.example.myapplication;

public class Car {
    final Engine engine;
    public Car(Engine engine) {
        this.engine = engine;
    }
}

File: ExampleUnitTest
package com.example.myapplication

import io.mockk.every
import io.mockk.mockk
import org.junit.Test

import org.junit.Assert.*

class ExampleUnitTest {

    @Test
    fun unableToStubBehavior() {
        val car: Car = mockk(relaxed = true){}
        every { car.engine }.returns(mockk{})
        assertEquals(6, car.engine.cylinderCount)
    }
}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
joxlfg2qcommented, Nov 24, 2022

Having the same issue with Android’s ViewBinding. Here’s a simple reproducible sample:

public class Foo {
    public String string;
}
    @Test
    fun foo() {
        val foo: Foo = mockk()
        every { foo.string } returns "something" // <- Exception: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock

        assertEquals("something", foo.string)
    }

However, if you add a getter to the Java class, the test will work fine:

public class Foo {
    private String string;
    
    public String getString() {
        return this.string;
    }
}
1reaction
ScottSusmancommented, Jul 12, 2022

In case I messed something else going from my actual code to the minimum reproducible, here’s the actual code I’m using. It’s failing in the test setup, not even getting to the actual test. I’m wanting to test the viewHolder to ensure I’m binding the right data into the right textfield.

class TeeboxViewHolderTest {

    private lateinit var viewHolder: TeeboxAdapter.TeeboxViewHolder
    private lateinit var binding: GolfSimTeeboxItemBinding  // This is an android generated ViewBinding (not data binding, if that's relevant)

    @Before
    fun setup() {
        binding = mockk(relaxed = true)
        every { binding.root }.returns(mock{})
        every { binding.teeBoxNameText }.returns(mockk{}) // Line 39
        val context = mockk<Context> {
            val mockResources = mockk<Resources> {
                every {getDimensionPixelSize(any())}.answers { arg(0) }
            }
            every { resources }.returns(mockResources)
        }
        viewHolder = TeeboxAdapter.TeeboxViewHolder(binding, context)
    }

    @Test
    fun `test that isn't player specific`() {
        val data = TeeboxAdapter.TeeboxAdapterData("name", emptyList(), mock{}) {}
        viewHolder.bind(data)
        assertThat(binding.teeBoxNameText.text, isEqualTo("name"))
        assertTrue(binding.root.hasOnClickListeners())
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

android - Mockk Missing calls inside every { ... } block
It gets mocked without issues, but I can't run my test as JVM; running androidTest inside Foo module. Got the same crash; using...
Read more >
Verify that functions were called | Mocking | MockK Guidebook
When using mocked dependencies, you usually want to test that your code calls the correct functions. In MockK, this is accomplished using the...
Read more >
mockk-io/Lobby - Gitter
I am trying to mock an object however I keep getting the following error io.mockk.MockKException: Missing calls inside every { ... } block....
Read more >
MockK: A Mocking Library for Kotlin - Baeldung
A typical mocked object will throw MockKException if we try to call a method where the return value hasn't been specified. If we...
Read more >
Stubbing and Mocking with Mockito and JUnit - Semaphore CI
Mocking is the act of removing external dependencies from a unit test in order to create a controlled environment around it. Typically, we...
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