31 skills · 85 min
Skills
Skill 5 of 31
Define and generate mock objects for external dependencies using package:mockito and build_runner.
2 minutes · 399 words · 11 sections
Install
npx skills add flutter/agent-plugins --skill dart-generate-test-mocksnpx skills add flutter/agent-plugins/plugin marketplace add flutter/agent-pluginsThe first command installs just this skill, by the name in its SKILL.md; the second installs the whole repository.
Design Dart classes to support dependency injection. Isolate complex external dependencies (like API clients or databases) so they can be replaced with mock objects during testing.
http.Client) through class constructors.Uri objects using Uri.parse(string).Configure the pubspec.yaml file with the necessary testing and code generation packages.
package:http) using dart pub add http.dart pub add dev:test dev:mockito dev:build_runner.import 'package:http/http.dart' as http;.Use package:mockito and build_runner to automatically generate mock classes for fixed scenarios and behavior verification.
@GenerateNiceMocks annotation (preferable to @GenerateMocks to avoid missing stub exceptions).MockSpec<Type>() objects..mocks.dart extension.build_runner to generate the mock files: dart run build_runner build.Isolate the system under test using the generated mock objects. Use package:test to structure the test suite.
when(mock.method()).thenReturn(value) for synchronous methods.thenAnswer((_) async => value) for methods returning a Future or Stream. Never use thenReturn for asynchronous returns.verify(mock.method()).called(1) to check exact invocation counts.any, anyNamed, or captureAny for flexible verification.Use the following checklist to implement and verify mocked unit tests.
http.Client).target_test.dart) and add @GenerateNiceMocks([MockSpec<Dependency>()]).part or import directive for the generated .mocks.dart file.dart run build_runner build to generate the mock classes.group() and test().when().verify() and assert outcomes using expect().dart test.If tests fail or build_runner encounters errors:
dart test or dart run build_runner build.@GenerateNiceMocks.ArgumentError, change thenReturn to thenAnswer.build_runner fails, ensure the .mocks.dart import matches the file name exactly.1. System Under Test (lib/api_service.dart)
import 'dart:convert';
import 'package:http/http.dart' as http;
class ApiService {
final http.Client client;
ApiService(this.client);
Future<String> fetchData(String urlString) async {
final uri =
2. Test Implementation (test/api_service_test.dart)
import 'package:test/test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:http/http.dart' as http;
import 'package:my_app/api_service.dart';
// Generate the mock class for http.Client
Define and generate mock objects for external dependencies using `package:mockito` and `build_runner`. Use when unit testing classes that depend on complex external services like APIs or databases.
The verbatim description from this skill’s front matter — the string an agent matches on to decide whether to load it.
main, last pushed 17 September 2026.SKILL.md, not by matching a directory convention. 2 distinct layouts observed: .agents/agents/reidbaker-agent/skills/*/SKILL.md, skills/*/SKILL.md.h1 and no skipped levels:.claude-plugin/marketplace.json by Dart and Flutter Team, declaring 1 plugin. It is read for editorial metadata only — never as the skill index, which is always the repository tree./flutter/agent-plugins.md, and each skill at its own .md URL.