3

I use mockk for unit testing in Kotlin (Android).

I want to verify that a function is called:

    verify { obj.callSomething("param1", Param2("A", "B")) }

In this case Param2 is a generated Java class that doesn't override equals method so that the verification is always fail.

I've tried to use match but the failure message is simply not helpful.

    verify { obj.callSomething("param1", match { it.a == "A" && it.b == "B" }) }

Is there a better or correct way to do this?

fikr4n
  • 3,250
  • 1
  • 25
  • 46

1 Answers1

3

You can use withArg to run assertions and other arbitrary code on an argument in your verify call. Using assertEquals will give you better error messages.

verify {
  obj.callSomething("param1", withArg {
    assertEquals("A", it.a)
    assertEquals("B, it.b)
  })
}
NotWoods
  • 686
  • 4
  • 13