Flutter unable to get generate mocks with mockito generateMocks and json_serializable


I am trying to follow this example https://flutter.dev/docs/cookbook/testing/unit/mocking to create http unit tests. I have used json_serializer to create bridge between JSON and models. I made sure this library is null safety enabled

The command I am running is
flutter pub run build_runner build
and the stack trace is long and it won’t stop, seems like a forever loop

The stacktrace does’t have the exact line from my code so not sure where to go from here.

Note that I am not using http library hence I gave the model generated by json_serializer
enter image description here

Full output is here, I highlighted what I think is the main problem

[INFO] Generating build script...
[INFO] Generating build script completed, took 511ms

[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 10.5s

[INFO] There was output on stdout while compiling the build script snapshot, run with `--verbose` to see it (you will need to run a `clean` first to re-snapshot).

[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 529ms

[INFO] Checking for unexpected pre-existing outputs....
[INFO] Checking for unexpected pre-existing outputs. completed, took 1ms

[INFO] Running build...
[INFO] Generating SDK summary...
[INFO] 3.3s elapsed, 0/8 actions completed.
[INFO] Generating SDK summary completed, took 3.3s

[INFO] 4.4s elapsed, 4/8 actions completed.
[INFO] 5.4s elapsed, 4/8 actions completed.
[INFO] 13.2s elapsed, 7/8 actions completed.
[SEVERE] json_serializable:json_serializable on test/timeline_vm_test.dart:

***Null check operator used on a null value***
#0      LinkedElementFactory.isLibraryUri (package:analyzer/src/summary2/linked_element_factory.dart:418:48)
#1      LibraryContext.isLibraryUri (package:analyzer/src/dart/analysis/library_context.dart:95:27)
#2      LibraryAnalyzer._isLibrarySource (package:analyzer/src/dart/analysis/library_analyzer.dart:511:25)
#3      LibraryAnalyzer._resolveDirectives (package:analyzer/src/dart/analysis/library_analyzer.dart:552:36)
#4      LibraryAnalyzer.analyzeSync (package:analyzer/src/dart/analysis/library_analyzer.dart:128:5)
#5      LibraryAnalyzer.analyze (package:analyzer/src/dart/analysis/library_analyzer.dart:105:12)
#6      AnalysisDriver._computeAnalysisResult.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1590:63)
#7      PerformanceLog.run (package:analyzer/src/dart/analysis/performance_logger.dart:32:15)
#8      AnalysisDriver._computeAnalysisResult (package:analyzer/src/dart/analysis/driver.dart:1565:20)
#9      AnalysisDriver._computeErrors (package:analyzer/src/dart/analysis/driver.dart:1644:26)
#10     AnalysisDriver.performWork (package:analyzer/src/dart/analysis/driver.dart:1266:20)
#11     AnalysisDriverScheduler._run (package:analyzer/src/dart/analysis/driver.dart:2296:24)
<asynchronous suspension>

[INFO] 14.4s elapsed, 8/16 actions completed.
[INFO] 15.5s elapsed, 8/16 actions completed.
[INFO] 16.6s elapsed, 8/16 actions completed.
[INFO] 17.7s elapsed, 8/16 actions completed.
[INFO] 18.7s elapsed, 8/16 actions completed.
[INFO] 19.8s elapsed, 8/16 actions completed.
[INFO] 20.8s elapsed, 8/16 actions completed.
[INFO] 21.9s elapsed, 8/16 actions completed.
[INFO] 22.9s elapsed, 8/16 actions completed.
[INFO] 24.0s elapsed, 8/16 actions completed.
[INFO] 25.1s elapsed, 8/16 actions completed.
[INFO] 26.1s elapsed, 8/16 actions completed.
[INFO] 27.2s elapsed, 8/16 actions completed.
[INFO] 28.3s elapsed, 8/16 actions completed.
[WARNING] No actions completed for 15.0s, waiting on:
  - mockito:mockBuilder on test/timeline_vm_test.dart
  - mockito:mockBuilder on test/widget_test.dart
  - mockito:mockBuilder on lib/busy_notifier.dart
  - mockito:mockBuilder on lib/main.dart
  - mockito:mockBuilder on lib/data/timeline_vm.dart
  .. and 3 more


Turns out for my case, all I had to do was remove

#import '../lib/main.dart'

I am not sure why exactly but it worked. Will add more details if I find them.

Answered By – satyajit

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *