Skip to content
Development
Skill

/mockito

Use when generating mocks, stubbing methods, verifying interactions, capturing arguments, or choosing between mocks, fakes, and real objects (Mockito).

From plugin
flutter-ai-skills
67036 skills

Claude Code

$ npx -y skills add evanca/flutter-ai-rules --skill mockito --agent claude-code

SKILL.md

mockito.SKILL.md
name: mockito
description: "Use when generating mocks, stubbing methods, verifying interactions, capturing arguments, or choosing between mocks, fakes, and real objects (Mockito)."
license: MIT

Mockito Skill

This skill defines how to correctly use the `mockito` package for mocking in Dart and Flutter tests.

---

1. Mock vs. Fake vs. Real Object

| Use | When | |---|---| | **Real object** | Prefer over mocks when practical. | | **Fake** (`extends Fake`) | Lightweight custom implementation; override only the methods you need. Prefer over mocks when you don't need interaction verification. | | **Mock** (`extends Mock`) | Only when you need to **verify interactions** (call counts, arguments) or stub dynamic responses. |

  • **Data models** should not be mocked if they can be constructed with stubbed data.
  • Only use mocks if your test has `verify` assertions; otherwise prefer real or fake objects.

---

2. Generating Mocks

@GenerateMocks([MyClass])
// or for nice mocks (return simple legal values for missing stubs):
@GenerateNiceMocks([MockSpec<MyClass>()])
void main() { ... }
dart run build_runner build
  • Only annotate files under `test/` for mock generation by default.
  • Use a `build.yaml` if you need to generate mocks outside of `test/`.
  • Never add `@override` methods or implementations to a class extending `Mock`.
  • Never stub responses in a mock's constructor or inside the mock class — always stub in your tests.

---

3. Stubbing

final mock = MockCat();

// Return a value
when(mock.sound()).thenReturn('Meow');

// Throw an error
when(mock.sound()).thenThrow(Exception('No sound'));

// Calculate response at call time
when(mock.sound()).thenAnswer((_) => computedValue);

// Return values in sequence
when(mock.sound()).thenReturnInOrder(['Meow', 'Purr']);
  • Always stub methods/getters **before** using them if you need specific return values.
  • Missing stub behavior: `@GenerateMocks` → throws; `@GenerateNiceMocks` → returns a simple legal value.
  • Use `throwOnMissingStub(mock)` to throw on any unstubbed call.

---

4. Verification

verify(mock.sound());                  // called at least once
verifyNever(mock.eat(any));            // never called
verify(mock.sound()).called(2);        // called exactly twice

**Async:**

await untilCalled(mock.sound());       // wait for the interaction

---

5. Argument Matchers

// Flexible stubbing
when(mock.eat(any)).thenReturn(true);
when(mock.eat(argThat(isNotNull))).thenReturn(true);

// Named arguments
when(mock.fetch(any, headers: any)).thenReturn(response);
  • Do **not** use `null` as an argument adjacent to an argument matcher.
  • For named arguments, use `any` or `argThat` as values, not as argument names.

---

6. Capturing Arguments

final captured = verify(mock.eat(captureAny)).captured;
print(captured.last); // last captured argument

Use `captureThat` for conditional capturing.

---

7. Resetting Mocks

reset(mock);                  // clear all stubs AND interactions
clearInteractions(mock);      // clear only recorded interactions

---

8. Mocking Function Types

To mock a function type (e.g., a callback), define an abstract class with the required signature and generate mocks for it:

abstract class Callback {
  void call(String value);
}

@GenerateMocks([Callback])

---

9. Debugging

logInvocations([mock1, mock2]); // print all collected invocations

---

References

  • [Mockito GitHub Repository](https://github.com/dart-lang/mockito)
Read more
@evanca
Ships withflutter-ai-skills

36 Flutter and Dart skills your coding agent loads by itself, sourced only from official documentation. A skill is a folder with a SKILL.md file.

Stars on GitHub

Momentum

stars in total
670
Star history of evanca/flutter-ai-rules on GitHub, a chart by star-history.com

Other skills on flutter-ai-skills.

accessibility

accessibility

Use when working on accessibility, a11y, WCAG, ARIA, screen readers, keyboard nav, focus order, contrast, alt text, captions,…

bloc

bloc

Use when creating a Cubit or Bloc, modeling state with sealed classes or status enums, wiring…

dart-3-updates

dart-3-updates

Use when writing switch statements, refactoring if-else chains, creating data classes, choosing records vs classes, destructuring…

More skills from other plugins.

brainstorming
Auto-invoked

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or…

@obra@obraInstall Skill
ask-matt

ask-matt

Ask which skill or flow fits your situation. A router over the skills in this repo.

skills2w309k
@mattpocock@mattpocockInstall Skill
accessibility
Auto-invoked

accessibility

Design, implement, and audit accessible UI to WCAG 2.2 Level AA across Web, iOS, and Android — semantic ARIA roles and labels,…

ecc2w293k
@affaan-m@affaan-mInstall Skill
compiler-commit

compiler-commit

Use when you want to verify compiler changes and commit with the correct convention. Runs tests, lint, and format, then commits…

react5d252k
@facebook@facebookInstall Skill

Questions about mockito.

Which coding agents can use mockito?
mockito installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.