61 Mockito
61.1 GenerateMocks
The @GenerateMocks([http.Client])
annotation is a code generation directive that tells the mockito
package to automatically create mock classes for you.
61.1.1 What It Does
This annotation generates a mock version of http.Client
called MockClient
that you can use in your tests.
61.1.2 How It Works
1. You write the annotation:
import 'package:mockito/annotations.dart';
import 'package:http/http.dart' as http;
.Client])
@GenerateMocks([httpvoid main() {
// your tests
}
2. Run code generation:
dart run build_runner build
3. Mockito generates a file (usually your_test_file.mocks.dart
) containing:
// Auto-generated mock class
class MockClient extends Mock implements http.Client {
// All the methods from http.Client are implemented
// but they do nothing by default (return null/throw)
}
61.1.3 File Structure Example
test/
├── api_test.dart # Your test file with @GenerateMocks
├── api_test.mocks.dart # Auto-generated mock classes
└── ...
61.1.4 Usage in Tests
After generation, you can use the mock:
import 'api_test.mocks.dart'; // Import the generated mocks
void main() {
late MockClient mockClient;
{
setUp(() = MockClient(); // Use the generated mock class
mockClient });
'should make POST request', () {
test(// Now you can use when().thenAnswer() on mockClient
when(mockClient.post(/* ... */)).thenAnswer((_) async => mockResponse);
});
}
61.1.5 Why Use Code Generation?
Before (manual mocking):
class MockClient extends Mock implements http.Client {} // You write this
After (with (GenerateMocks?)): - Mockito writes it for you - Ensures all methods are properly implemented - Updates automatically when the original class changes - Reduces boilerplate code
61.2 Visual Process
@GenerateMocks([http.Client])
↓
dart run build_runner build
↓
Analyzes http.Client interface
↓
Generates MockClient class
↓
You import and use MockClient in tests
This approach is especially useful when mocking complex classes with many methods, as Mockito handles all the implementation details for you.