Skip to main content
skill CX Codex

mockito — Claude Code skill: what it does, how to install

mockito is a Claude Code skill for Dart and Flutter tests that guides mock generation, stubbing, and interaction checks. It activates for Mockito testing tasks.

MIT ★ 674 updated 2026-10-29

Install into Claude Code · Plugin marketplace All methods and platforms ↓
/plugin marketplace add evanca/flutter-ai-rules
/plugin install flutter-ai-skills@flutter-ai-rules
Open on GitHub

What mockito does

mockito helps choose a real object, a fake, or a mock for a test. Use mocks to verify interactions or supply changing responses; data models should instead be built with test values when practical.

The skill covers @GenerateMocks and @GenerateNiceMocks, followed by dart run build_runner build. Mock generation defaults to files under test/; generating mocks elsewhere requires build.yaml. It also covers stubbing, call verification, argument matchers, and captured arguments.

What it helps with

  • choose between mocks, fakes, and real objects
  • generate mock classes for Dart tests
  • stub return values and exceptions
  • verify calls and capture arguments

Example prompts for the agent

  • Show me how to verify a method's call count in a Dart test.
  • Generate mocks for these classes with Mockito.
  • Set up different return values for successive mock calls.

How to install mockito

1Plugin marketplace

/plugin marketplace add evanca/flutter-ai-rules
/plugin install flutter-ai-skills@flutter-ai-rules

This installs the whole flutter-ai-skills plugin with all its skills

2Skills CLI (Vercel)

npx skills add evanca/flutter-ai-rules --skill mockito -a claude-code

3Manually: put the files in place

~/.claude/skills/mockito/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/mockito/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about mockito

How do I install mockito in Claude Code?

Add the marketplace with /plugin marketplace add evanca/flutter-ai-rules, then install the plugin with /plugin install flutter-ai-skills@flutter-ai-rules.

What does mockito do?

It guides Mockito use in Dart and Flutter tests, including mock generation, stubbing, and interaction checks. It also explains when to use a mock, fake, or real object.

How can I generate mocks outside the test directory?

Mock generation outside test/ requires a build.yaml file.

Source text

SKILL.md · MIT
---
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

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

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

**Async:**

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

---

## 5. Argument Matchers

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

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

Use `captureThat` for conditional capturing.

---

## 7. Resetting Mocks

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

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

@GenerateMocks([Callback])
```

---

## 9. Debugging

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

---

## References

- [Mockito GitHub Repository](https://github.com/dart-lang/mockito)

© Ivanna Kaceviča · MIT · evanca/flutter-ai-rules

Similar skills

All skills for Testing and QA →

From the same set39

The whole set →
flutter-errors flutter-errors is a Claude Code skill that helps diagnose Flutter layout and runtime errors. It activates for RenderFlex overflow, scroll issues, and setState calls during build. CX Codex +1 ★ 674repo stars mocktail Guidance for using Mocktail in Dart and Flutter tests: configure stubs, inspect interactions, and decide when a mock, fake, or real object fits. CX Codex +1 ★ 674repo stars flutter-change-notifier flutter-change-notifier — a skill that teaches the agent to use ChangeNotifier and provider for Flutter state management: models, providers, Consumer, rebuild optimization. CX Codex +1 ★ 674repo stars provider Guides agents on architecting Flutter state with the provider package, keeping rebuilds minimal and dependencies reactive. CX Codex +1 ★ 674repo stars developing-genkit-dart developing-genkit-dart — a skill for building AI agents with Genkit Dart that tells the agent when to pull in reference docs for the core framework and its plugins. CX Codex +1 ★ 674repo stars firebase-storage firebase-storage is a skill with guidelines for Firebase Cloud Storage in Flutter apps, covering file uploads, downloads, metadata changes, and storage error handling. CX Codex +1 ★ 674repo stars firebase-data-connect Get guidance for Firebase Data Connect in Flutter, from GraphQL operations and typed SDK generation to access rules and offline handling. CX Codex +1 ★ 674repo stars firebase-remote-config A Flutter skill that keeps the agent on track with Firebase Remote Config: feature flags, in-app defaults, A/B tests and live config updates. CX Codex +1 ★ 674repo stars

Project sponsors

Companies that keep this analytics open