Making Assertions On A Stub For Consecutive Calls

İbrahim Gündüz
2 min readApr 10, 2022

When you need to write some unit tests for a piece of code like the following one, you need to make sure that the dependencies are called expectedly on each call in case of repetitive calls like recursion.

As you can see in the example code above, the method returns the parsed response if the page count equals the current page, otherwise, it calls itself recursively. So, in case of recursion, we need to make sure that the method and the mocked dependencies are called with the correct parameters on each call in the tests.


We need some dependencies for the example project.

Creating Stubs and Dummy Objects

As the recursion case occurs when thex-total-pages key from the response headers is received with a value bigger than one, we can start to write out test code by creating dummy response objects like the following ones.

As the resultset consists of two pages, the request builder object should return some results like the following ones according to the page number it is called with.

And.. we have some parsed responses and final result expectations like the following