Coroutine/suspend function mocking support
See original GitHub issueIf you use non-inline lambdas that are not declared as suspend, the runBlocking { } around the test function doesn’t help, rather we need to mock { on { runBlocking { suspendFun() } } }
for each mock…
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:19
Top Results From Across the Web
Mocking and stubbing suspend functions with MockK
How to easily stub or mock suspend function in Kotlin? MockK is the solution. See MockK examples for coroutines testing.
Read more >How to test suspend function using MockK? - Stack Overflow
runBlocking allows you to call suspend functions by blocking a new coroutine and it blocks the current thread until it is completed.
Read more >Mocking Coroutines - ProAndroidDev
Except for the keyword suspend , this looks like is a synchronous function with a direct return type. Let's get back to Mockito...
Read more >Coroutine/suspend function mocking support · Issue #205
If the mock and onGeneric lambdas were either suspend functions or inline, then I wouldn't need to put runBlocking inside the onGeneric for ......
Read more >Coroutines and suspend functions | Mocking | MockK Guidebook
Using coEvery, coVerify, and more to mock coroutines. ... MockK uses function literals to create stubs, small changes are needed to stub suspend...
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
I don’t really use coroutines much myself. I can suggest this solution:
… but I don’t know if it covers all scenarios. The following test passes:
Pointing to this version I was able to use directly the onBlocking function: “com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-alpha04”
I tried using latest version: “com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-RC1” but onBlocking is not available anymore and I don’t know if there is another function that is replacing this.