Cherry Zhang
Cherry Zhang

Reputation: 101

Mockito verify doesn't match multiple stub

It seems that, when I use thenReturn like this:

when(abc.call()).thenReturn(a).thenReturn(b),

I expect:

verify(abc, times(2)).call()

instead, the method seems only get called once, I am a little confused(my test work as expected, the mock seems return the value I expected), but for the invocation times, I don't know if I am getting the wrong result, or it's a expected behavior of the Mockito?

Upvotes: 1

Views: 630

Answers (1)

Deb Das
Deb Das

Reputation: 294

when() is mocking the abc.call() method which has to produce a certain return type. If the method is called once, it will return the value also once, so the stubbing for method invocation is done only once hence the verify only recognizes one call.

You need to customize your function so that the stub(abc.call()) gets called more than once.

You can follow this thread for implementing multiple stubs

Upvotes: 0

Related Questions