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.

mockito-kotlin cannot verify higher-order functions in Robolectric Tests

See original GitHub issue

@nhaarman , the area that I was trying to test was like this

val testArg = mock<(String) -> Unit>()
val result = someClass.doSomething(testArg)
verify(testArg).invoke(any())

And the error comes out as the folllwing

org.mockito.exceptions.misusing.UnfinishedVerificationException: 
Missing method call for verify(mock) here:
-> at com.nhaarman.mockitokotlin2.VerificationKt.verify(Verification.kt:42)

Example of correct verification:
    verify(mock).doSomething()

Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.

	at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:44)
	at com.hootsuite.testing.rule.RxJavaSchedulerRule$apply$1.evaluate(RxJavaSchedulerRule.kt:31)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:253)
	at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
	at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
...
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)
	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 com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:9
  • Comments:11

github_iconTop GitHub Comments

9reactions
jachenrycommented, Sep 6, 2018

We’re seeing the same issue in our project. Our workaround is to create a test only interface that satisfies the higher-order function and can be verified. Using your example it’d look like:

val testCallback = mock<Callback>()
val result = someClass.doSomething(testCallback::invoke)
verify(testCallback).invoke(any())

interface Callback {
  operator fun invoke(value: String)
}

Less than ideal but the only workaround we’ve found. Please let me know if there is an easier way.

5reactions
jenzzcommented, Jul 22, 2019

@jachenry @davidxiasc

Simply declaring a private interface Callback : () -> Unit is sufficient as a workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mockito-kotlin cannot verify higher-order functions in ...
mockito-kotlin cannot verify higher-order functions in Robolectric Tests.
Read more >
kotlin - Is there a way to verify that a top-level function passed ...
This can be achieved by mocking the higher-order function as higher-order functions ... @Mock val testUiPrinter: (String) -> Unit @Test fun ...
Read more >
Is possible to finish unit testing when a method is called?-kotlin
I think there is a basic problem with your test setup: You do not use verify to check if one function calles another...
Read more >
Android testing: Mockito and Robolectric (Part 2)
Mockito allows use static mock() method for creating mock objects. MyClass test = Mockito.mock(MyClass.class);</pre> // or @RunWith( ...
Read more >
kotlinx-coroutines-test
runTest is the way to test code that involves coroutines. suspend functions can be called inside it. IMPORTANT: in order to work with...
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