How to Use Mockito with Flutter for Effective Unit Testing
To use
mockito with Flutter, add the mockito package to your dev_dependencies in pubspec.yaml. Then create mock classes by extending Mock and use them in your tests to simulate dependencies without real implementations.Syntax
Mockito in Flutter uses mock classes to simulate real objects in tests. You create a mock by extending the Mock class and implementing the interface or class you want to mock. Then you use when() to define behavior and verify() to check interactions.
dart
import 'package:mockito/mockito.dart'; class MockService extends Mock implements Service {} void main() { final mockService = MockService(); when(mockService.fetchData()).thenReturn('Mock Data'); print(mockService.fetchData()); // prints 'Mock Data' verify(mockService.fetchData()).called(1); }
Output
Mock Data
Example
This example shows how to mock a simple service class in Flutter tests using Mockito. It demonstrates creating a mock, setting up a return value, calling the method, and verifying the call.
dart
import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; // Service to be mocked class ApiService { String fetchData() => 'Real Data'; } // Mock class class MockApiService extends Mock implements ApiService {} void main() { test('MockApiService returns mocked data', () { final mockApi = MockApiService(); when(mockApi.fetchData()).thenReturn('Mocked Data'); final result = mockApi.fetchData(); expect(result, 'Mocked Data'); verify(mockApi.fetchData()).called(1); }); }
Output
All tests passed.
Common Pitfalls
- Not adding
mockitotodev_dependenciescauses import errors. - For null safety, generate mocks using
build_runnerandmockitoannotations instead of extendingMockdirectly. - Forgetting to call
verify()to check if mocked methods were called. - Using real implementations instead of mocks can slow tests and cause flaky results.
dart
/* Wrong way: Extending Mock without generating mocks in null-safe projects */ class WrongMock extends Mock implements ApiService {} /* Right way: Use @GenerateMocks and build_runner to generate mocks */ import 'package:mockito/annotations.dart'; @GenerateMocks([ApiService]) void main() {} // Then run: flutter pub run build_runner build
Quick Reference
Here is a quick cheat sheet for common Mockito usage in Flutter:
| Action | Syntax | Description |
|---|---|---|
| Create mock class | class MockX extends Mock implements X {} | Create a mock for class/interface X |
| Stub method | when(mock.method()).thenReturn(value); | Define what the mock returns when method is called |
| Verify call | verify(mock.method()).called(n); | Check method was called n times |
| Clear interactions | clearInteractions(mock); | Reset recorded calls on mock |
| Generate mocks (null safety) | @GenerateMocks([X]) + build_runner | Generate mocks with annotations for null safety |
Key Takeaways
Add mockito to dev_dependencies and import it in your test files.
Create mock classes by extending Mock or using @GenerateMocks with build_runner for null safety.
Use when() to define mock behavior and verify() to check method calls.
Avoid using real implementations in unit tests to keep tests fast and reliable.
Run flutter pub run build_runner build to generate mocks when using annotations.