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

dan.the.man

08/21/2022, 9:38 PM
Is there any known weirdness with the latest stable versions of kotlin/coroutines and Mockito? I'm having an issue where my tests are provided mocks, the debugger confirms it's a mock, but the code actually tries to execute in those mocks
j

Jacob

08/21/2022, 9:58 PM
I'm skeptical that you're describing the problem correctly. Are you using mockito-kotlin? plain old mockito runs into trouble because the matchers all return null and assign the value to possibly non-nullable args. I'm a big fan mockK https://mockk.io/
d

dan.the.man

08/21/2022, 10:02 PM
Yes, I'm using mockito-kotlin. I have a VM, where I have a
Repo
which only has
Api
as a dependency of it.
Copy code
lateinit var repo: Repo
   lateinit var vm: VM

repo = mock {onblocking{this.myApiCall()}.thenReturn(result)
vm = VM(repo)

vm.doMethod()
This results in a NPE where
Copy code
open class Repo(api:Api){
  fun myApiCall(){
    val apiResult =  api.makeCall() //Results in NPE
But I would expect for the mock to intercept this and instead just return the value I specified
If I take it a step further, and instead of making the Repo as a mock, but instantiate a real one, and then mock the
Api
then I'm seeing the correct behavior
5 Views