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:
- Created 5 years ago
- Reactions:9
- Comments:11
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Less than ideal but the only workaround we’ve found. Please let me know if there is an easier way.
@jachenry @davidxiasc
Simply declaring a
private interface Callback : () -> Unit
is sufficient as a workaround.