Flutter Mockito Package Cheat Sheet

Introduction Link to heading

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.

How to Mock Dependencies in your Flutter App for Testing Link to heading

Here is a tutorial on how to create mocks with the mockito package, set them up, and use them in your tests of a Flutter application.

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 Link to heading

✅ 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 Link to heading

You can find the source code on GitHub.

How to write basic unit tests for a Flutter application Link to heading

We cover a basic example of how unit tests in a Flutter project can look like. Explained code samples are included.

How to Show Test Coverage of a Flutter App in Visual Studio Code Link to heading

Here is a short guide about how to visualize code coverage of a Flutter app in Visual Studio Code.

How to use animations and animated widgets in Flutter apps Link to heading

Here’s how you can animate your Flutter widgets to make your app stand out from the rest with ease.