Bort
Bort

Reputation: 97

How to Mock recursive function

When I test a method the inner of the method invokes itself and another method that is in the same class. I use a partial mock to specify the other method's return value but how do I specify the first method's return value?

Upvotes: 0

Views: 2309

Answers (2)

bomxp
bomxp

Reputation: 11

lets try returnsMany in mockk:

coEvery{ mockEntity.recursiveMethod()}.returnsMany(value1, value2,...)

The first call recursiveMethod() will return value1, then the second call return value2,...

Handle your recursive flow in the right way

Upvotes: 0

dkatzel
dkatzel

Reputation: 31648

If you are mocking the method, then it is no longer recursive - because the mock will only return the final return value that would be returned after recursion.

If you want to test the recursive function, then don't mock the recursive method.

Your explanation is a little unclear, but perhaps just mocking the other method that is called is sufficient for your test. You can make sure that other method gets called with the correct parameters.

Upvotes: 2

Related Questions