Developer journal: Dart Mockito Error, No stub was found …

Mario Gunawan
2 min readOct 28, 2022

--

Earlier today, while working with Mockito + GetIt, I stumbled upon a strange error when mocking one of my method

No stub was found which matches the arguments of this method call.

I already used the when() expression that mocks the method, so I’m wondering what went wrong. Here is my test and codes:

when(GetIt.instance.get<RestClient>().fetchItems()).thenAnswer( ... );

And the code that is calling it is:

final items = await GetIt.instance.get<RestClient>().fetchItems( options: BaseOptions());

Welp, you might have seen it, a shameful error of mine, where I mock the fetchItems without argument although the code is fetchItems with arguments

And of course I spent more than 2 hours debugging this

How to fix this error? Simple, use the anyNamed method provided by mockito library

when(GetIt.instance.get<RestClient>().fetchItems(options: anyNamed('options'))).thenAnswer( ... );

the mockito library also features a lot of argument matchers that can match any arguments that you write, you just need to read the docs xd.

If this doesn’t solve your problem, check if you have “when( … )” expression calling your code. Or maybe check if you have mocked your class.

If you stumbled upon this problem, I hope you don’t spend 2 hours of your precious time like I did. Stay safe traveller, stay safe….

this article is written when mockito is version 5.3.2, flutter is version 3.3.5, and dart is version 2.18.2

--

--

Mario Gunawan

I'm a mobile / web developer. I'm mostly writing articles about software development.