Flutter test : mockito GenerateMocks vs GenerateNiceMocks

Flutter test : mockito GenerateMocks vs GenerateNiceMocksImage by brgfx on FreepikStubbing is the best way to do unit test. It will make the unit test run faster and isolated. We can do stubbing by mocking the object ( class ) that we do not want to te…


This content originally appeared on Level Up Coding - Medium and was authored by Michael Febrianto

Flutter test : mockito GenerateMocks vs GenerateNiceMocks

Image by brgfx on Freepik

Stubbing is the best way to do unit test. It will make the unit test run faster and isolated. We can do stubbing by mocking the object ( class ) that we do not want to test. Mockito package is suggested by flutter to achieve this.

If we read the flutter cookbook especially on mock dependencies. It is suggested that we are using GenerateMocks annotation to create a test file with a mock:

However, if we open mockito documentation. It is suggested that we are using GenereateNiceMocks instead:

The detail of GenerateMocks and GenerateNiceMocks can be read in this documentation. Let me summarize and show you the different in practical code. We start with the class that we would like to test. We have screen to read a book. We would like to do a simple unit tests on that class.

You do not need to read the class below as a whole. Please focus on the LibraryService. We are going to setup mock for LibraryService to isolate ReadScreen class unit test. After that we can compare the different between the old GenerateMocks and the new GenerateNiceMocks

The mock

Please check the generated mock file and different between GenerateMocks and GenerateNiceMocks.

as you can see clearly there is new named-parameter called returnValueForMissingStub. This means with GenerateNiceMocks : all method of mocked class is stubbed even though you didn’t put the code explicitly in the unit test.

See in Action

We know now the different in code. Let us see this in action by removing the stubbing in the unit test and run the test.

GenerateNiceMocks after stub is removed
GenerateMocks result after stub is removed

The unit test with GenerateNiceMocks will run successfully. However the unit test with GenerateMocks will throw an error for this particular test.

TL;DR

The conclusion, please use @GenerateNiceMocks since it will reduce your code and mistake on unit test. @GenerateNiceMocks will make your flutter unit test more robust by stubbing all mocked class automatically. Furthermore @GenerateMocks will be deprecating.

That’s all for now. Thank you for reading. God bless you all.


Flutter test : mockito GenerateMocks vs GenerateNiceMocks was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Michael Febrianto


Print Share Comment Cite Upload Translate Updates
APA

Michael Febrianto | Sciencx (2023-03-10T14:47:29+00:00) Flutter test : mockito GenerateMocks vs GenerateNiceMocks. Retrieved from https://www.scien.cx/2023/03/10/flutter-test-mockito-generatemocks-vs-generatenicemocks/

MLA
" » Flutter test : mockito GenerateMocks vs GenerateNiceMocks." Michael Febrianto | Sciencx - Friday March 10, 2023, https://www.scien.cx/2023/03/10/flutter-test-mockito-generatemocks-vs-generatenicemocks/
HARVARD
Michael Febrianto | Sciencx Friday March 10, 2023 » Flutter test : mockito GenerateMocks vs GenerateNiceMocks., viewed ,<https://www.scien.cx/2023/03/10/flutter-test-mockito-generatemocks-vs-generatenicemocks/>
VANCOUVER
Michael Febrianto | Sciencx - » Flutter test : mockito GenerateMocks vs GenerateNiceMocks. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/10/flutter-test-mockito-generatemocks-vs-generatenicemocks/
CHICAGO
" » Flutter test : mockito GenerateMocks vs GenerateNiceMocks." Michael Febrianto | Sciencx - Accessed . https://www.scien.cx/2023/03/10/flutter-test-mockito-generatemocks-vs-generatenicemocks/
IEEE
" » Flutter test : mockito GenerateMocks vs GenerateNiceMocks." Michael Febrianto | Sciencx [Online]. Available: https://www.scien.cx/2023/03/10/flutter-test-mockito-generatemocks-vs-generatenicemocks/. [Accessed: ]
rf:citation
» Flutter test : mockito GenerateMocks vs GenerateNiceMocks | Michael Febrianto | Sciencx | https://www.scien.cx/2023/03/10/flutter-test-mockito-generatemocks-vs-generatenicemocks/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.