Mockito stale mock silent trycatch failure

From divinevideo/divine-mobile · View interactive version

Mockito stale mock silent trycatch failure is an AI agent skill from the GitHub repository divinevideo/divine-mobile. Fix misleading Flutter/Dart test failures where expected data is empty ([]) or default values instead of the mocked response, caused by stale Mockito generated mocks or missing stubs being silently swallowed by try/catch in provider/repository code. Use when: (1) Test expects non-empty data but gets

How to use this skill

Skill file

Mockito Stale Mock + Silent Try/Catch Test Failures

Problem

When a PR adds new methods to a service class (e.g., AnalyticsApiService), and those

methods are called from existing code paths that are wrapped in try/catch blocks, tests

fail with misleading symptoms. The test assertion shows Expected: non-empty, Actual: []

or Expected: true, Actual: false — but the actual root cause is a MissingStubError

being silently caught.

Context / Trigger Conditions

The Failure Chain

1. PR adds method `getBulkVideoViews()` to AnalyticsApiService
2. PR adds `_enrichVideosWithBulkStats()` to HomeFeedProvider.build()
3. _enrichVideosWithBulkStats calls getBulkVideoViews (new method)
4. Generated mock is stale — doesn't have getBulkVideoViews at all
5. Even if regenerated, test setUp doesn't stub getBulkVideoViews
6. Mock throws MissingStubError when unstubbed method is called
7. Provider's try/catch catches ALL exceptions (including MissingStubError)
8. Provider falls back to empty state → test sees [] instead of expected data

Solution

Step 1: Regenerate mocks

dart run build_runner build --delete-conflicting-outputs

This updates all .g.dart and .mocks.dart files to include new methods.

Step 2: Identify new method calls in modified code paths

Trace the code path from the test's entry point. Look for any method calls on mocked

services that were added or modified by the PR. Pay special attention to methods called

after the already-stubbed method (e.g., enrichment/post-processing steps).

Step 3: Add stubs for ALL methods in the call chain

// In test setUp():

// Existing stub (was already there)
when(mockService.getHomeFeed(
  pubkey: anyNamed('pubkey'),
  limit: anyNamed('limit'),
)).thenAnswer((_) async => HomeFeedResult(videos: mockVideos));

// NEW stubs needed for enrichment methods added by PR
when(mockService.getBulkVideoStats(
  argThat(isA<List<String>>()),  // non-nullable positional param
)).thenAnswer((_) async => <String, BulkVideoStatsEntry>{});

when(mockService.getBulkVideoViews(
  argThat(isA<List<String>>()),  // non-nullable positional param
  maxVideos: anyNamed('maxVideos'),
  maxConcurrent: anyNamed('maxConcurrent'),
)).thenAnswer((_) async => <String, int>{});

Step 4: Handle non-nullable parameters

Mockito's any returns null which fails for non-nullable params. Use:

(but NOT for non-nullable named params without defaults)

Verification

After fixing:

  1. Run the specific test file: flutter test test/path/to/test.dart
  2. Confirm previously failing assertions now pass
  3. Run full test suite to check for no regressions

Example

Before (failing):

Expected: non-empty
  Actual: []

  test/providers/home_feed_loading_fix_test.dart 253:9

Root cause: _enrichVideosWithBulkStats called getBulkVideoStats (unstubbed) →

MissingStubError → caught by provider try/catch → empty fallback state.

Fix: Added stubs in setUp() for getBulkVideoStats and getBulkVideoViews.

After (passing): All 11 tests pass, including the 2 that were failing.

Notes

The .mocks.dart files must match the current interface.

useless if the provider then calls enrichVideos which calls 2 more unstubbed methods.

production error handling. You'll never see it unless you add temporary print

statements inside the catch block or use @GenerateNiceMocks (which returns defaults

instead of throwing, potentially masking different bugs).

level. For non-nullable positional params, use argThat(isA<Type>()) which returns a

properly typed matcher.

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.