https://kotlinlang.org logo
#coroutines
Title
# coroutines
b

bdawg.io

08/21/2018, 2:31 PM
@Martin Devillers have you tried verifying the 2nd argument passed is a
Continuation
?
m

Martin Devillers

08/21/2018, 2:32 PM
How would I do that?
b

bdawg.io

08/21/2018, 2:37 PM
use a matcher that specifies it should be an instance of a
Continuation
(I’m not sure what the matcher is called, I don’t have a project using Mockito right now) Something along the lines of
Copy code
Mockito.verify(mock).invoke(any(), instanceOf(Continuation::class.java))
m

Martin Devillers

08/21/2018, 2:39 PM
I don’t know that there’s any way to pass the continuation argument for a suspending function explicitly. AFAIK it’s all managed by the compiler. That’s essentially the magic of coroutines.
b

bdawg.io

08/21/2018, 2:41 PM
You don’t need to.
runBlocking
is executing the block as a coroutine. The compiler will automatically pass the Continuation. You just need to verify it was passed. I looked up the matcher as well:
Copy code
Mockito.verify(mock).invoke(any(), any(Continuation::class.java))
https://static.javadoc.io/org.mockito/mockito-core/2.21.0/org/mockito/ArgumentMatchers.html#any-java.lang.Class-
m

Martin Devillers

08/21/2018, 2:48 PM
The compiler won’t let you write that, because in that code
invoke
is the method on the instance, which doesn’t have a
Continuation
instance visible within Kotlin. It’s added by the compiler.
b

bdawg.io

08/21/2018, 2:51 PM
Does
Mockito.verify(mock)
return
SuspendingAction
as a proxy? I’m not very familiar with Mockito specifically
m

Martin Devillers

08/21/2018, 2:53 PM
public static <T> T verify(T mock)
it returns an instance of the class being verified, which is actually the mock itself decorated in order to perform assertions on it. You then call the instance methods on it, and Mockito verifies that they were also called on the mock itself.
b

bdawg.io

08/21/2018, 2:58 PM
Ahh. This would be outside the area that I can help 🙂. My GUESS would be something to with the
any()
matcher and instantiating an instance of
Any()
directly 🤷 best of luck on it though!
m

Martin Devillers

08/21/2018, 2:59 PM
Thanks for trying!
g

groostav

08/21/2018, 11:56 PM
@bdawg.io I havent tried this but my understanding of mockito's architecture implies that it might work:
Copy code
val verifier = Mockito.verify(mock)
val arg1Ignored = any(firstArgType::class)
val arg2Ignored = any(Continuation::class)
verifier.invoke(arg1Ignored)
to my knowledge this is because mockito uses a static stack behind the scenes to track argument matchers and invocations. In this way, we're tricking that system into effectivly pushing an extra argument matcher for
Continuation
onto the stack. IIRC the actual value passed into the method invocation is erroneous
er... no its going to complain about mixing-and-matching live with matcher arguments isnt it... damn.
m

Martin Devillers

08/22/2018, 7:56 AM
😖
4 Views