In this article, we are going to present a way to verify the order of method calls in Mockito tests. We will show a special use of an InOrder object in conjunction with the verify method
2. Verify the order of method calls
To verify the order of calls Mockito provides a special object InOrder.
Let's just straight to the example, to better understand how to use it:
In this example, we used the InOrder class of Mockito to verify the order of method calls on the mocked Map object.
If the order is not correct we will get an exception similar to this:
This example was simple, and use only a single mock object. However, Mockito.inOrder(...) allows configuring more than one mock.
Let's now check a more complex example with two mock objects:
In this example, we have SomeClass with the doSomething() method that calls methods on mocked objects. In the Mockio.inOrder(..) method we are passing two mock objects: map and list. Then we used the inOrder.verify(...) method for verification the order of method calls and input params.
3. Conclusion
In this article, we presented a simple method to verify the order of method calls. This kind of verifications is useful in tests where the calling order of methods is also important not only the number.
Test snippets used in this article are available in our GitHub repo.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}