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.

Bug: Cannot mock static function with Robolectric 4.4

See original GitHub issue

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 ] -----------------------

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:15

github_iconTop GitHub Comments

2reactions
Raibazcommented, Mar 16, 2021

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.

0reactions
kingargylecommented, Mar 30, 2021

@Raibaz the open issue if you want to comment on it on Robolectric is here. https://github.com/robolectric/robolectric/issues/5994

Read more comments on GitHub >

github_iconTop Results From Across the Web

Robolectric and Powermock attempting to mock static method ...
I am using Robolectric 2.1, Powermock 1.5.1 and i would like to simply write a static method test. i just want to verify...
Read more >
Shadows - Robolectric
Shadow objects are not quite Proxies, not quite Fakes, not quite Mocks or Stubs. Shadows are sometimes hidden, sometimes seen, and can lead...
Read more >
Android Unit Testing with Robolectric - Florence Njeri
Mockito is a framework used to mock Android classes. It helps to test implementation details and not the behavior. Robolectric creates an ...
Read more >
Development history - JMockit - GitHub Pages
If a class needs to have only some constructors and/or static methods mocked, the use of a MockUp is the recommended alternative. See...
Read more >
Jacoco VerifyError with synchronized blocks [36995132]
I am instrumenting an app with Soot and I have the same verifyerror problem caused by the synchronized blocks. I use a helper...
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