Flutter Mockito Package Cheat Sheet

Flutter category image

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.

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.

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.


Want More Flutter Content?

Join my bi-weekly newsletter that delivers small Flutter portions right in your inbox. A title, an abstract, a link, and you decide if you want to dive in!

Flutter ❤️ Firebase

Get started with Firebase and learn how to use it in your Flutter apps. My detailed ebook delivers you everything you need to know! Flutter and Firebase are a perfect match!

Become A Testing Expert!

Become a proficient Flutter app tester with my detailed guide. This ebook covers everything from unit tests over widget tests up to dependency mocking.