Has someone an working example of a mocked class w...
# announcements
r
Has someone an working example of a mocked class where you want to verify that one given method was called once? I tried
verify(sut).someMethod("example")
, but that throws always 3 invocations (two other where on other methods). I have right now no idea how to fix that test
b
You can pass a
VerificationMode
to your
verify()
function call to do your own assertions on the method interaction
You can use
times()
to assert number of times the method is called
r
So you mean basically that I should use the second parameter of verify (e.g.
verify(sut, times(3)).someMethod("example")
)? If so that does not work for me. I could only set it to 3 because in total 3 methods are called on that mocked class, but I could not verify which method which makes it worthless. Especially want to verify the passed parameters, that seems not to work at all. The method definition looks like that:
fun somMethod(label: String, flag: Boolean = false) {...}
m
Mockito should be verifying that
sut.someMethod("eample")
was called only once (the default for
times
) using your initial definition. I don't know what, but something isn't defined/configured correctly. Can you point us to more code? And we're all assuming you're using Mockito. Let us know if it's Mockk or something else. And you're 100% sure your method isn't being called 3 times somehow?
b
You can use an
ArgumentCaptor
instead of the actual parameters to
someMethod()
to verify what they are in your test.