# Mockito

Use when generating mocks, stubbing methods, verifying interactions, capturing...

- Repository: https://github.com/evanca/flutter-ai-rules (668 stars)
- Category: Engineering
- License: MIT
- Last updated: 2026-10-30
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add evanca/flutter-ai-rules --skill mockito -g -a claude-code -y`
Codex: `npx skills add evanca/flutter-ai-rules --skill mockito -g -a codex -y`
Cursor: `npx skills add evanca/flutter-ai-rules --skill mockito -g -a cursor -y`
Gemini CLI: `npx skills add evanca/flutter-ai-rules --skill mockito -g -a gemini-cli -y`
Claude Code plugin marketplace: `/plugin marketplace add evanca/flutter-ai-rules`

## Description

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

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# 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

```dart
@GenerateMocks([MyClass])
// or for nice mocks (return simple legal values for missing stubs):
@GenerateNiceMocks([MockSpec<MyClass>()])
void main() { ... }
```

```bash
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

```dart
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

The rest: https://github.com/evanca/flutter-ai-rules/blob/HEAD/skills/mockito/SKILL.md
