Szymon Jeziorski
02/26/2022, 1:01 PMverify
- verifying method call with the argument being an object equal to another object comparing field by field but ignoring one or more fields.
So far the only reasonable solution I came up with was creating a matcher
using AssertJ's usingRecursiveComparison()
and wrapping it to `runCatching`:
inline fun <reified T : Any> MockKVerificationScope.matcherFor(expected: T, vararg ignoredFields: String) = match<T> {
runCatching {
assertThat(it).usingRecursiveComparison().ignoringFields(*ignoredFields).isEqualTo(expected)
}.isSuccess
}
example usage:
verify {
userRepository.save(matcherFor(expectedUser, "id"))
}
I found the above to be elegant enough from the calling side but was wondering if there would be any more straightforward way to achieve the same with less overhead at the same time.
Thanks in advance for any suggestions!Mattia Tommasone
02/26/2022, 4:07 PMval slot = slot<YourType>()
verify {
userRepository.save(capture(slot))
}
val captured = slot.captured
assertEquals(captured.someProperty, "expectedValue")
...
Szymon Jeziorski
02/26/2022, 4:17 PMmatcher
rather than slot
as I want to check if the method was called with some particular argument but I'm not assuming it was the first/only call to this method