Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
See original GitHub issueJust 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:
- Created a year ago
- Comments:5
Top GitHub Comments
Having the same issue with Android’s ViewBinding. Here’s a simple reproducible sample:
However, if you add a getter to the Java class, the test will work fine:
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.