{"slug":"mockito","title":"mockito","summary":"Use when generating mocks, stubbing methods, verifying interactions, capturing arguments, or choosing between mocks, fakes, and real objects (Mockito).","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-08-30T09:57:20.162296Z","repo":{"url":"https://github.com/evanca/flutter-ai-rules","stars":642,"forks":66,"license":"MIT","updatedAt":"2026-09-14T07:51:38Z"},"bodyHtml":"<hr>\n<h2>name: mockito\ndescription: \"Use when generating mocks, stubbing methods, verifying interactions, capturing arguments, or choosing between mocks, fakes, and real objects (Mockito).\"\nlicense: MIT</h2>\n<h1>Mockito Skill</h1>\n<p>This skill defines how to correctly use the <code>mockito</code> package for mocking in Dart and Flutter tests.</p>\n<hr>\n<h2>1. Mock vs. Fake vs. Real Object</h2>\n<table>\n<thead>\n<tr>\n<th>Use</th>\n<th>When</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><strong>Real object</strong></td>\n<td>Prefer over mocks when practical.</td>\n</tr>\n<tr>\n<td><strong>Fake</strong> (<code>extends Fake</code>)</td>\n<td>Lightweight custom implementation; override only the methods you need. Prefer over mocks when you don't need interaction verification.</td>\n</tr>\n<tr>\n<td><strong>Mock</strong> (<code>extends Mock</code>)</td>\n<td>Only when you need to <strong>verify interactions</strong> (call counts, arguments) or stub dynamic responses.</td>\n</tr>\n</tbody>\n</table>\n<ul>\n<li><strong>Data models</strong> should not be mocked if they can be constructed with stubbed data.</li>\n<li>Only use mocks if your test has <code>verify</code> assertions; otherwise prefer real or fake objects.</li>\n</ul>\n<hr>\n<h2>2. Generating Mocks</h2>\n<pre><code>@GenerateMocks([MyClass])\n// or for nice mocks (return simple legal values for missing stubs):\n@GenerateNiceMocks([MockSpec&lt;MyClass&gt;()])\nvoid main() { ... }\n</code></pre>\n<pre><code>dart run build_runner build\n</code></pre>\n<ul>\n<li>Only annotate files under <code>test/</code> for mock generation by default.</li>\n<li>Use a <code>build.yaml</code> if you need to generate mocks outside of <code>test/</code>.</li>\n<li>Never add <code>@override</code> methods or implementations to a class extending <code>Mock</code>.</li>\n<li>Never stub responses in a mock's constructor or inside the mock class — always stub in your tests.</li>\n</ul>\n<hr>\n<h2>3. Stubbing</h2>\n<pre><code>final mock = MockCat();\n\n// Return a value\nwhen(mock.sound()).thenReturn('Meow');\n\n// Throw an error\nwhen(mock.sound()).thenThrow(Exception('No sound'));\n\n// Calculate response at call time\nwhen(mock.sound()).thenAnswer((_) =&gt; computedValue);\n\n// Return values in sequence\nwhen(mock.sound()).thenReturnInOrder(['Meow', 'Purr']);\n</code></pre>\n<ul>\n<li>Always stub methods/getters <strong>before</strong> using them if you need specific return values.</li>\n<li>Missing stub behavior: <code>@GenerateMocks</code> → throws; <code>@GenerateNiceMocks</code> → returns a simple legal value.</li>\n<li>Use <code>throwOnMissingStub(mock)</code> to throw on any unstubbed call.</li>\n</ul>\n<hr>\n<h2>4. Verification</h2>\n<pre><code>verify(mock.sound());                  // called at least once\nverifyNever(mock.eat(any));            // never called\nverify(mock.sound()).called(2);        // called exactly twice\n</code></pre>\n<p><strong>Async:</strong></p>\n<pre><code>await untilCalled(mock.sound());       // wait for the interaction\n</code></pre>\n<hr>\n<h2>5. Argument Matchers</h2>\n<pre><code>// Flexible stubbing\nwhen(mock.eat(any)).thenReturn(true);\nwhen(mock.eat(argThat(isNotNull))).thenReturn(true);\n\n// Named arguments\nwhen(mock.fetch(any, headers: any)).thenReturn(response);\n</code></pre>\n<ul>\n<li>Do <strong>not</strong> use <code>null</code> as an argument adjacent to an argument matcher.</li>\n<li>For named arguments, use <code>any</code> or <code>argThat</code> as values, not as argument names.</li>\n</ul>\n<hr>\n<h2>6. Capturing Arguments</h2>\n<pre><code>final captured = verify(mock.eat(captureAny)).captured;\nprint(captured.last); // last captured argument\n</code></pre>\n<p>Use <code>captureThat</code> for conditional capturing.</p>\n<hr>\n<h2>7. Resetting Mocks</h2>\n<pre><code>reset(mock);                  // clear all stubs AND interactions\nclearInteractions(mock);      // clear only recorded interactions\n</code></pre>\n<hr>\n<h2>8. Mocking Function Types</h2>\n<p>To mock a function type (e.g., a callback), define an abstract class with the required signature and generate mocks for it:</p>\n<pre><code>abstract class Callback {\n  void call(String value);\n}\n\n@GenerateMocks([Callback])\n</code></pre>\n<hr>\n<h2>9. Debugging</h2>\n<pre><code>logInvocations([mock1, mock2]); // print all collected invocations\n</code></pre>\n<hr>\n<h2>References</h2>\n<ul>\n<li><a href=\"https://github.com/dart-lang/mockito\">Mockito GitHub Repository</a></li>\n</ul>\n","files":[{"path":"SKILL.md","sizeBytes":3623,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-08-30T09:58:37.61215Z","sha256":"47CE31008AC16A5496E08761C3CC6F88F89BC78B7F9D0414CED2910FD68A1D43","sizeBytes":1733},"review":null,"source":{"repositoryUrl":"https://github.com/evanca/flutter-ai-rules","path":"skills/mockito","license":"MIT","commit":"7b9cce235714ae17acbae896b1e8c8627e128f4c","subtreeSha":"47B7BCA142F86792328F18DABD59B3FA6D386A87E5D672D387B278CAC7EF04C5","lastSyncedAt":"2026-09-22T13:50:26.675606Z"},"reviewedAt":"2026-08-30T10:15:03.443773Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/evanca/flutter-ai-rules/tree/main/skills/mockito"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install evanca-flutter-ai-rules@llmmart"},{"target":"git","command":"git clone https://github.com/evanca/flutter-ai-rules.git"}]}