Mockito + Unit Testing in Flutter 101

This thread talks about Verify and Stubbing using Mockito๐Ÿงต

#flutter #flutterdev
If you are pretty new to Unit Testing, I suggest you read through my previous thread on Unit Testing:

So, what is Mockito and why do we need to use them while testing?

๐Ÿ‘‡๐Ÿป
Mockito is a package that lets you create Mock Dependencies while testing your units.

It helps in testing the functionality of a class in isolation. It does its processes uses dummy input rather than connecting to an external db or file server for actual data.
So how do we do Mocking in Flutter ?

We use the package: mockito

Add it to dev_dependencies in pubspec.yaml along with the latest version of build_runner.

Note: If you are facing any errors while adding the packages, feel free to try any older versions
We create a new file in the test folder and name it cats.dart

( I am following the example from the official docs of mockito)

Add the following code: Image
You will get errors for the import and the declaration statement after you paste the code.

Thatโ€™s alright. Next, go to your terminal and build the code using the command below: Image
This will generate the cat.mocks.dart file which will contain the MockCat class with pre-filled code.

This is the Mock class that we are gonna use for the entire test cycle.
Now you can go ahead and write your tests using this instance object.
But, there are two concepts you need to know before writing tests. They are :

1. Verify

2. Stubbing
Verify is used to check whether a particular method was called on a mock object using the given parameters.

Letโ€™s see an example: Image
The test will pass if the method mentioned inside verify was called earlier.

But note that the method should have the same parameter list, or else the test will fail.
Now, lets see what is Stubbing.

Check out the code below:

The method soundWithParam will return the same string which we pass as the parameter right?

What if you wanna change its behaviour while testing?

๐Ÿ‘‡๐Ÿป Image
What if you wanna make the method return โ€œFlutterโ€ for any input string ?

We perform Stubbing.

Check the code below: Image
This alters the return statement when you call the method soundWithParam with any parameter.

So every time you call the method with any string as a parameter, it will return โ€œFlutterโ€ as output.
Well this is the magic of Mockito. But this is not all that this package is capable of, to know more about the package, check out its official documentation:

pub.dev/packages/mockiโ€ฆ
I know this was just a basic overview of the package. I will try to write an article on this exploring all the possible functionalities of the package and share it soon.

If you liked this thread, donโ€™t forget to like and retweet it, so that it reaches more amazing devs like you!
Stay connected with me as I share my learnings with Dart and Flutter ๐Ÿ’™

Donโ€™t forget to hit the notify button so that you get my contents right in your inbox!

twitter.com/GopinathanAswin

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Aswin Gopinathan ๐Ÿ’™๐Ÿ‡ฎ๐Ÿ‡ณ

Aswin Gopinathan ๐Ÿ’™๐Ÿ‡ฎ๐Ÿ‡ณ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @GopinathanAswin

17 Dec
Everything you need to know about Widget Lifecycle in Flutter

A thread ๐Ÿงต

#flutter #flutterdev
There are two types of Widgets in Flutter:

1. Stateless Widget : They do not change their state throughout the lifetime of your application. They are like a constant widget.

2. Stateful Widget: They are dynamic, they change their state based on the External Interactions.
A side note: Stateless widgets can change their state if some state management tools are used like Provider, Bloc, etc

But thatโ€™s out of the scope of this thread.

Letโ€™s continue ๐Ÿ‘‡๐Ÿป
Read 19 tweets
14 Dec
Unit Testing in Flutter 101

A mega thread ๐Ÿงต

#flutter #flutterdev
Unit Testing is the most important testing method, that is usually performed by the developer.

Unit Testing tests each individual modules (method, function or a class) for its proper functioning.
This indirectly means you have to maintain loose coupling in your application.

That is, each function or a method should be able to perform only one functionality.

This way it will be easier to test and even debug you codebase.
Read 19 tweets
13 Dec
In my latest tweet, I talked about how you can prevent multiple calls to your future methods using a variable approach.

What if I say, there is one more efficient approach to it!

Thanks to @DmytroGladkyi for telling me about it

Read on to know about it ๐Ÿ‘‡๐Ÿป

#flutterdev #flutter
The approach is using the AsyncMemoizer class.

To use the class, import the following package:
Now, letโ€™s create an instance of the AsyncMemoizer class, and initialise it inside the initState method (or you can initialise during declaration).
Read 7 tweets
13 Dec
When working with Futures and FutureBuilders, it is recommended not to execute the Future methods inside the FutureBuilder.

I learned it the hard way!

Read on to know why ๐Ÿ‘‡๐Ÿป

#flutter #flutterdev
Every time your UI refreshes (eg: using a setState), the future method is run again, which can turn out to be very expensive!

Sometimes this could be helpful, but most of the times itโ€™s a curse for the application.

So how can you fix it ?

๐Ÿ‘‡๐Ÿป
We need to set the future in such a way that its run only once, and will never run again until you re-open the screen or you explicitly define a Swipe-down-to-refresh feature.

So, what is that one thing in Flutter, which will run only once in the entire life-cycle of your screen
Read 10 tweets
12 Dec
Future v/s Streams

A restaurant analogy thread ๐Ÿงต๐Ÿ‘‡๐Ÿป

#flutter #flutterdev
Imagine you go to a restaurant and your date gives you two options:

1. Takeout and eat at your home

2. Find a table and eat there itself

Well these options in the Flutter world means:

1. Future

2. Stream
Okay! Lets see the first option: Takeout

You ordered:

1. Burgers ๐Ÿ”
2. Fries ๐ŸŸ
3. Pizza ๐Ÿ•

The waiter gave you a token and asked you to wait.

<Few moments later>

Your takeaway is ready and you both leave the place.
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(