Introduction
This article is all about the possibilities of mocking that the Mockito package provides. We’ll look into setting up mocks, verifying the calls, and pitfalls to avoid.
I recently published an article about mocking in unit tests with the Mockito package in Flutter apps.
![Flutter category image](https://quickcoder.org/wp-content/uploads/2023/01/fluttercat-150x150.png)
This follow-up article is a cheat sheet for you to quickly find an example of how to use the package. I also updated the GitHub repository of the original post and it now contains the cheat sheet as a working test class.
Get the Flutter Test Guide for free!
![Flutter Test Guide Ebook Cover](https://www.quickcoder.org/wp-content/uploads/2022/01/Cover-213x300.png)
An ebook about unit and widget testing in Flutter applications. Boost your testing skills now with this guide!
Final tips
✅ Always use thenAnswer()
, never use thenReturn()
▶ Explanation
✅ If you verify a mock, the invocation counter is reset!
✅ If there are multiple matching setups, the last setup wins!
Source code
You can find the source code on GitHub.
Related articles
![Flutter category image](https://quickcoder.org/wp-content/uploads/2023/01/fluttercat-150x150.png)
![Flutter category image](https://quickcoder.org/wp-content/uploads/2023/01/fluttercat-150x150.png)