Hi all! I am using mockk for tests but keeps faili...
# mockk
v
Hi all! I am using mockk for tests but keeps failing to mock the calls that I expect, when I try running all tests from cli (with gradle:
gradle :test
). when run individually with
kotest
runs perfectly. I have something like this:
Copy code
beforeSpec {
  Application.start()
  mockkObject(MyObjectClass)
  every { MyObjectClass.getRandomString() } returns "call made"
})

afterSpec {
   clearAllMocks()
   Application.stop()
}

....
// some other tests that should make calls to the function that I mocked above

"verify mockk" {
   verify(exactly = 7) {
        MyObjectClass.getRandomString()
   }
}
I can't figure out what could be wrong here, I get this error when run from the command line :
java.lang.AssertionError: Verification failed: call 1 of 1: MyObjectClass(object MyObjectClass).getRandomString()) was not called  .......
Any help is appreciated, thank you! 🙏
m
i admit i’m not a big kotest expert, but i think it may be because the order of execution of tests is not guaranteed, so it may be the case that your “verify mockk” test is executed before others
as a general rule, i’d put individual verifications in each test rather than having a global verification for all of them
does this make sense to you?
v
yep, this makes total sense. Will try it out, thanks 🙏
m
let me know if it works 🙂
v
same error 😔
m
ouch 😞
s
I'm not a mockk user but do you have to assign the result of the mockkobject call to something?
v
if I run it individually works, without the assignment. But I have played around with this more, and it turns out this happens because the order of tests gets mixed up when run from cli, and also that function was supposed to be triggered from a coroutine, which doesn't work well from cli, where multiple coroutines get started for each test.
m
yep it kind of makes sense. Ideally, the order of execution of your tests should not affect their outcome, which means they should not affect other tests in the same suite or scenario
👍 1