Mockk verify fails when checking called and wasNot called
Asked Answered
E

1

4

I am trying to verify that a function was not called using the following:

verify {
    managementService.deleteUser(any()) wasNot Called
}

That verification fails with the message:

Verification failed: call 1 of 1: ManagementService(#11).deleteUser(any())) was not called.

If I invert the verification to this:

verify {
    managementService.deleteUser(any())
}

I still receive the same failure message.

There are other functions on ManagementService that pass wasNot Called just fine.

Why would my verification failing for wasNot Called, while the error message says the failure is because it was not called? And why would inverting the check produce the same error?

Esquire answered 5/1, 2023 at 23:1 Comment(1)
Does this answer your question? How to check if a method was not invoked with mockk?Lexicography
U
9

wasNot Called is not used to verify that a specific function call has not been made, but that an entire mock was never called, like this:

verify {
    managementService wasNot Called
}

If you want to verify that deleteUser was not called with any argument, you can verify that the call happened exactly zero times:

verify(exactly = 0) {
    managementService.deleteUser(any())
}
Usk answered 6/1, 2023 at 8:0 Comment(1)
This solves it. I see it in the docs now too (notwoods.github.io/mockk-guidebook/docs/mocking/verify/…). That didn't jump out at me before because I'm pretty new to Kotlin and Mockk and I was mimicking other existing tests. I'm still a little puzzled because the existing tests passed. For example, verify { managementService.createUser(any()) } passed verification. I initially included my original check in that same block but moved it to its own verify block to troubleshoot. Both cases, createUser(any()) passes but deleteUser(any()) fails.Esquire

© 2022 - 2024 — McMap. All rights reserved.