mockk: Bug: Cannot mock static function with Robolectric 4.4

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

Mock static function should success when using Robolectric 4.4

Current Behavior

Mock static function failure when using Robolectric 4.4

Failure Information (for bugs)

Stacktrace:

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 pkg.FooTest.test(FooTest.kt:60)
	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.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
	at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:263)
	at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

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 10.15.6 (19G2021)
  • Kotlin version: 1.3.72
  • JDK version: 11
  • JUnit version:
  • Robolectric version: 4.4
  • Type of test: Robolectric unit test

Minimal reproducible code (the gist of this issue)

// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
    testImplementation "org.robolectric:robolectric:4.4"
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------

// Test target (pkg.Fookt):

class Foo
fun Foo.test() = "foo"

// Test code
@RunWith(AndroidJUnit4::class)
class FooTest {

  @Test
  fun test() {
    mockkStatic("pkg.FooKt")
    val foo: Foo = mockk()
    every { foo.test() } returns "bar" // Fail here
    println(foo.test())
    // Remove @RunWith(AndroidJUnit4::class) will print bar
  }
}

// -----------------------[ YOUR CODE ENDS HERE ] -----------------------

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 10
  • Comments: 15

Most upvoted comments

Looks like robolectric/robolectric#5754 is actually excluding the whole io.mockk package from compilation, so it’s unlikely this can be solved by changing Mockk.

I’d be glad to get in touch with the Robolectric team, though.

In the meantime, closing this issue as it has a workaround.