dart-lang / mockito

Mockito-inspired mock library for Dart
https://pub.dev/packages/mockito
Apache License 2.0
623 stars 160 forks source link

Cannot create Mock in Dart 3.x: type 'Null' is not a subtype of type 'IOSink' #741

Closed haintt86 closed 3 months ago

haintt86 commented 3 months ago

Hello,

I am using mockito for mocking File and IOSink to test, and recent got the error:

type 'Null' is not a subtype of type 'IOSink'

When using Dart SDK sdk: '>=3.3.2 <4.0.0' and mockito: 5.4.4 with below example code is has error:

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'dart:io';

class MockFile extends Mock implements File {}
class MockIOSink extends Mock implements IOSink {}

void main() {
  test("Write file", () async {
    MockFile mockFile = MockFile();
    MockIOSink mockSink = MockIOSink();
    when(mockFile.openWrite()).thenReturn(mockSink);
  });
}

Environment info:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.4, on macOS 14.4.1 23E224 darwin-arm64, locale en-CA)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)

When using older Dart version sdk: ">=2.7.0 <3.0.0" and mockito: ^5.2.0 the code above does not have any issue.

Is there any proper way to create mock for File and IOSink like above? Thank you

srawlins commented 3 months ago

There is, please see the README for how to create mocks with the null safety language feature.