How can I verify that a constructor is called with...
# mockk
m
How can I verify that a constructor is called with certain parameters? I thought that after setting up
mockkConstructor()
I could verify the call with
constructedWith()
, but it's not working. Mockk is complaining:
Missing calls inside verify { ... } block.
Currently my test has this setup:
Copy code
init {
  mockkConstructor(Context::class)
}

@Test
fun `ctor is called` {
  //constructor is called through a factory function...

  verify {
    constructedWith<Context>(
      EqMatcher("default"),
      // other matchers...
    )
  }
}
m
ah, that’s a tough one, i don’t think you can verify constructors
h
m
i don’t think there’s an easy way to do that, because you’d have to tell the classloader not to call the actual constructor when you’re in a verify block but instead just record the call
m
Thanks for confirming guys, sadly I found out myself after too many failed attempts. I even tried to mockkStatic() the primary constructor and verify its call but it didn't work. In the end I had to change the test and assert over stringified version of the instance. Thanks anyway!
m
generally speaking, constructors and mocking together is always a painful combo
so in this case i won’t say “i’d gladly review a PR about it if you want to implement constructor verification” because I know it’s not going to be fun 😄
m
I agree haha