Search code examples
unit-testingkotlinmockk

mocking only one call at a time with mockk


I know that in order to mock how a method responds, you have to use

every { instanceX.methodB() } returns "42"

I'm trying to mock an iterator, for which you have to mock 2 methods hasNext() and next(), if hasNext() returns true always there will be an infinite loop, if it returns false from the beginning, next() will not return anything.

My question is: is there a way to mock individual calls one by one with mockk, as you can do in mockito ? I couldn't find anything in the docs.


Solution

  • Use returnsMany or andThen construct with/instead of return.