argThat
The argThat
argument matcher in Mockito lets you create advanced argument matchers that run a function on passed arguments, and checks if the function returns true
.
If you have a complicated class that can’t be easily checked using .equals()
, a custom matcher can be a useful tool.
`when`(
mockedCar.drive(argThat { engine -> engine.dieselEngine })
).thenReturn(true)
MockK has a similar argument matcher called match. Just like argThat
, you can pass a lambda in that returns a boolean.
every {
mockedCar.drive(match { engine -> engine.dieselEngine })
} returns true
MockK also lets you use coroutines with match
. Just replace the function with coMatch
, then pass a suspend function lambda in. See Coroutines and suspend functions for more details.
当前内容版权归 Tiger Oakes 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Tiger Oakes .