Unit Testing with Mockito in Flutter

  Рет қаралды 26,307

Tadas Petra

Tadas Petra

Күн бұрын

Unit Testing with Mockito in Flutter
A couple videos ago we gave a short intro to Unit Testing. However it was missing a really big part! You can't do much Unit Testing without being able to Mock! In this example we will mock firebase auth, which enables us to run tests for all the login functions.
Widget Testing: • Widget Testing with Fl...
Integration Testing: • Integration Testing wi...
Link to the Code: github.com/tad...
Connect With Me
✦ My Website: [tadaspetra.com/](tadaspetra.com/#/)
✦ Live Coding: [ / tadaspetra ]( / tadaspetra )
✦ Twitter: [ / tadaspetra ]( / tadaspetra )
✦ Chat on Discord: [ / discord ]( / discord )
✦ Instagram: [ / tadaspetra ]( / tadaspetra )
I also do lots of Investing
✦ FREE stock: [join.robinhood...](join.robinhood...)
My Top 5 Books:
✦ Brave New World - Aldous Huxley: [amzn.to/2JyaK96](amzn.to/2JyaK96)
✦ 1984 - George Orwell: [amzn.to/3lmLzUk](amzn.to/3lmLzUk)
✦ The Millionaire Fastlane - MJ Demarco: [amzn.to/2ViGthn](amzn.to/2ViGthn)
✦ The 4-Hour Workweek - Timothy Ferriss: [amzn.to/3mnypYD](amzn.to/3mnypYD)
✦ Homo Deus - Yuval Noah Harari: [amzn.to/2VlFRYe](amzn.to/2VlFRYe)
KZfaq Gear
✦ MacBook Pro 16 inch: [amzn.to/3qe4MeF](amzn.to/3qe4MeF)
✦ MX Master 3 Mouse: [amzn.to/2VlkZQS](amzn.to/2VlkZQS)
✦ Canon Rebel SL3: [amzn.to/2ViIfPv](amzn.to/2ViIfPv)
✦ Hiearcool USB Hub: [amzn.to/3llsIsP](amzn.to/3llsIsP)
✦ iPad Pro 12.9 (3rd Generation): [amzn.to/36kaVy0](amzn.to/36kaVy0)
✦ Apple Pencil (2nd Generation): [amzn.to/2JoHy4y](amzn.to/2JoHy4y)
✦ Yeti Microphone: [amzn.to/37oRNOq](amzn.to/37oRNOq)
✦ Rode Boom Arm: [amzn.to/3lnEKCb](amzn.to/3lnEKCb)
✦ Blue Radius II Shock Mount: [amzn.to/2JoIfea](amzn.to/2JoIfea)
✦ Blue RPop Filter: [amzn.to/39rQSiZ](amzn.to/39rQSiZ)
✦ Sony WH1000XM3: [amzn.to/3moWgHh](amzn.to/3moWgHh)
Thank you for watching!
#unittesting #fluttertesting #testing

Пікірлер: 51
@abraiyan7984
@abraiyan7984 3 жыл бұрын
This video is a gem. This guy is teaching some valuable skills that's much more important than UI. People are just watching tutorials of beautiful UI. That's why they got more views without even talking and he got less views. His explanation style is very useful. I am sure you are not doing this just for views. Thanks for teaching us what you know. You are awesome.
@tadaspetra
@tadaspetra 3 жыл бұрын
That means a lot! Thank you! And yes my first priority is to actually teach people about house to make good software. The views would be nice as well though 😂
@rebarius
@rebarius 3 жыл бұрын
Oh Tadas, I love your videos! Great work man!
@tadaspetra
@tadaspetra 3 жыл бұрын
Thank you so much!
@RobertBrunhage
@RobertBrunhage 4 жыл бұрын
Awesome video! Another video tip could be showing different naming conventions for tests! That way it's easier to understand the context of the test without having to read through the whole test code
@tadaspetra
@tadaspetra 4 жыл бұрын
Robert Brunhage great too! What naming conventions do you use?
@RobertBrunhage
@RobertBrunhage 4 жыл бұрын
@@tadaspetra I am usually using Given .... When .... Then ... (dots are the explainations) But a lot of people skip the "Given" and just go with "When" and "Then". But there are a lot of good ones and haven't had the time to look in to many others hehe
@tadaspetra
@tadaspetra 4 жыл бұрын
Robert Brunhage I'll take a look at what is recommended😊. At work we had one test per function so we used the function name for the test name so it's easy to find with a quick search. But I think what you recommended is more useful
@RobertBrunhage
@RobertBrunhage 4 жыл бұрын
​@@tadaspetra Yes but the problem with that is when you rename the method you also have to rename the test. Making the tests dependant on other things than the actual logic that you test, if that makes sense. There is also the cases where you want to test a method multiple times like you did in the video!
@tadaspetra
@tadaspetra 4 жыл бұрын
@@RobertBrunhage Yes those are very good points. Thanks!
@TemiCodes
@TemiCodes 3 жыл бұрын
Wow. Thank you Tadas! I love this video. I’ve just learnt something from it. Thank you
@g4mrplays
@g4mrplays 3 жыл бұрын
Just going through your content, it's interesting how you get stuff straight to the point and it feels like you actually learned something compared to a lot of the stuff I've seen put out since 2018. Maybe this will force flutter content creators to finally step up w/ quality over quantity lol or you will single-handedly dominate this area. On top of that, most are extremely short videos and of things people need to actually know about.
@tadaspetra
@tadaspetra 3 жыл бұрын
Wow, that might be the best comment I have ever received! But those things you mentioned are exactly my goals for this channel. I personally don't think I can have an attention span of more then 10-15 minutes. So that's how long I will always try to make my videos unless it's a really complex topic. And then the second point, I think the best way for people to learn is to start a project and learn as you do it. Whenever they get to a section that they are not sure about or need a refresher, hopefully my videos will be a quick way to really get to understand the topic, where they will be able to build upon it with this new understanding. I really appreciate your comment, and I will always keep learning!
@uobislrt
@uobislrt 3 жыл бұрын
Great video Tadas. Thank you for sharing. I found it succinct but also easy to understand.
@tadaspetra
@tadaspetra 3 жыл бұрын
Thank you so much!
@franklinoladipo2343
@franklinoladipo2343 Жыл бұрын
Thank you. You explained it so well.
@jrheisler
@jrheisler 4 жыл бұрын
Stepping up the game, nice!!!
@tadaspetra
@tadaspetra 4 жыл бұрын
j heisler only way to go is up 😊
@MrZawe
@MrZawe 4 жыл бұрын
Nice more of what others rarely teach
@tadaspetra
@tadaspetra 4 жыл бұрын
Zawe yea I haven’t seen much about mockito and how to use it. I struggle with it still a bit
@karolisabrutis6501
@karolisabrutis6501 4 жыл бұрын
YUH YUH YUH!!
@fatima-cf1bj
@fatima-cf1bj Жыл бұрын
thank you so much I learned a lot from you 😍🙏
@tadaspetra
@tadaspetra Жыл бұрын
Thank you for watching :)
@longtimedeveloper6498
@longtimedeveloper6498 2 жыл бұрын
Thank you for a splendid tutorial. What equipment or software did you use to make the hand drawings at the beginning of the video? Super nice, thank you!
@tadaspetra
@tadaspetra 2 жыл бұрын
I used OneNote on an iPad 😊
@longtimedeveloper6498
@longtimedeveloper6498 2 жыл бұрын
@@tadaspetra Thank you!
@kirill4531
@kirill4531 3 жыл бұрын
Basically you're testing the Mockito lib and not the actual ability to create user/sign in/ sign out. If the Firebase falls you'll still have all your tests green. If someone completely changes the auth provider to, say, some custom backend, your tests still will be green, regardless of whether that custom auth fails or not.
@tadaspetra
@tadaspetra 3 жыл бұрын
correct. But thats exactly what should happen with unit testing. Unit testing tests the "unit" so it tests that the logic within the code still works. If something changes in the backend, you can run your tests, and they will still pass. So you know it is not your code that's broken There are 3 types of tests however, Integration can test things outside of your app, but it is still recommended to not do that. You don't need to be testing outside code, and if you are writing a custom auth, you should probably make sure you are either still giving the same inputs, or updating your code to retrieve those inputs
@kirill4531
@kirill4531 3 жыл бұрын
@@tadaspetra meh As for me I would rather create couple of integration tests that can definitely tell me whether the app is working correctly or not. Instead of tons of small unit tests which won't fail even when the app is crashed.
@tadaspetra
@tadaspetra 3 жыл бұрын
@@kirill4531 you can test however you want, but that wouldn’t be the “proper” way that they do it in corporations. With a couple integrations tests you’ll be able to test things that you are expecting throughout the app, but then if somehow one of your functions throws an error that crashes your app, you wouldn’t catch that without a unit test
@tomasbaran985
@tomasbaran985 7 ай бұрын
​@@tadaspetraI think what @kirill4531 is trying to say is the following: Unit testing the boundary (the outter layer/service layer) does not test/prove/verify anything but the Mockito package itself-in other words this kind of unit test is pointless. Why? Because it is not testing your auth.dart file at all since you are mocking the outputs. So, let's say you have a bug in auth.dart but this unit test will not catch it since you are mocking the results anyhow. What is rather recommended is testing e.g. biz logic/controllers/states/etc. When they call the outter layer (services), we mock them instead of testing them. The only wise way to test the boundaries (outter layers that communicate with the outside world) is via Integration testing. My 2 cents. I would love to hear more comments on that since I'm also learning about that.
@yulkin2002
@yulkin2002 3 жыл бұрын
Thanks for posting Tadas. How would you go about testing this code if your Auth class constructor did not take in FirebaseAuth as a parameter, but had it instead declared and initialized inside the class?
@tadaspetra
@tadaspetra 3 жыл бұрын
That’s actually a way that testing drives good architecture. When you are having it declared inside you are not following encapsulation and abstraction principles. So you can’t really make mocks if it is inside. But also it is better code design to not put it inside. This is a complex topic to explain in text haha I hope you understand
@yulkin2002
@yulkin2002 3 жыл бұрын
@@tadaspetra thanks!
@tadaspetra
@tadaspetra 3 жыл бұрын
@@yulkin2002 your welcome 😊
@danielolavioferreira599
@danielolavioferreira599 4 жыл бұрын
Great video, will you make one on using get for unit testing, you mentioned it on a previous video
@tadaspetra
@tadaspetra 4 жыл бұрын
I think so! But also I am thinking about it, and it might not be necessary? Like instead of testing the Auth class like we did here we would just test the GetXController class. And it shouldn't be much more complex
@juliosena6406
@juliosena6406 4 жыл бұрын
Just Amazing! Although it would be good to show the final coverage like => flutter test --coverage && genhtml coverage/lcov.info -o coverage/html
@tadaspetra
@tadaspetra 4 жыл бұрын
Julio Seña wow I completely forgot we have coverage reports too. Thank you for this comment!!
@juliosena6406
@juliosena6406 4 жыл бұрын
@@tadaspetra thanks for the videos. Top 1.
@JETLAI123
@JETLAI123 4 жыл бұрын
Nice setup
@tadaspetra
@tadaspetra 4 жыл бұрын
Thank you 🙌
@mosaed_b
@mosaed_b 3 жыл бұрын
That is so nice, thank you Tadas Is there a way to do it using GetX package?
@tadaspetra
@tadaspetra 3 жыл бұрын
Yes definitely. So any state management solution will help with unit testing because you can load your own state. And GetX I think has some test features that make it even easier
@paanoop
@paanoop 3 жыл бұрын
Hi Player! did I ever tell you you're cool! Love ya friend from India
@danielmorrissey6333
@danielmorrissey6333 3 жыл бұрын
Great video. I was not able to get Mockito to actually throw a FirebaseAuthException. In the debug state it says its the mockFirebaseAuth, and the onStateChanged test passes, but the Success string is always returned.
@danielmorrissey6333
@danielmorrissey6333 3 жыл бұрын
Your code works, there was a bug in mine that I could never find.
@GAMEOLOGIST
@GAMEOLOGIST 2 жыл бұрын
Actually, what is mocking ? I didn't understand yet. Could please explain in simple word ?
@riderprem
@riderprem 3 жыл бұрын
Great work mate! can you make a video on code coverage for the same in windows
@tadaspetra
@tadaspetra 3 жыл бұрын
I will take a look
@MrVipulLal
@MrVipulLal Жыл бұрын
Your video is out of date. The mocks are now generated automatically with @GenerateMocks.
Widget Testing with Flutter
8:16
Tadas Petra
Рет қаралды 29 М.
Unit Tests and Mocking with Flutter
12:29
Tadas Petra
Рет қаралды 11 М.
Люблю детей 💕💕💕🥰 #aminkavitaminka #aminokka #miminka #дети
00:24
Аминка Витаминка
Рет қаралды 662 М.
Unveiling my winning secret to defeating Maxim!😎| Free Fire Official
00:14
Garena Free Fire Global
Рет қаралды 11 МЛН
What will he say ? 😱 #smarthome #cleaning #homecleaning #gadgets
01:00
Integration Testing with Flutter
10:41
Tadas Petra
Рет қаралды 13 М.
Flutter Unit Testing Explained | Testing Series Codepur.dev
21:44
Flutter Unit Testing Tutorial For Beginners - Practical Guide
1:06:54
Rivaan Ranawat
Рет қаралды 34 М.
Flutter: BLoC Testing in 10 Minutes
13:49
Code With Cypert
Рет қаралды 1,5 М.
Can you run JavaScript in Flutter?
7:31
Andrei Lupsa
Рет қаралды 9 М.
Unit Testing - For Beginners in Dart - Setup with Flutter Project
15:02
Flutter Explained
Рет қаралды 24 М.
Unit Testing with Mockito | Flutter
19:34
Jignesh Patel
Рет қаралды 6 М.
Automate your Testing on Devices with Flutter Integration Testing
20:33
Flutter Explained
Рет қаралды 48 М.
Люблю детей 💕💕💕🥰 #aminkavitaminka #aminokka #miminka #дети
00:24
Аминка Витаминка
Рет қаралды 662 М.