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.

Get the Flutter Test Guide for free!

Flutter Test Guide Ebook Cover

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.


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!