''Null' is not a subtype of type 'Stream<int>' in type cast cubit(Bloc) flutter

I have created a cubit testing project in flutter which is working fine, but when I am writing a UI test case with mockito for the same it is throwing the following error. 'Null' is not a subtype of type 'Stream' in typecast. If the real object is being passed then the unit test is working fine.

MyCubit myCubit = MyCubit(); //real object working fine with UT

MyCubit myCubit = MockMyCubit(); //mocked object not working fine with UT.

Previously the same code was working with mockito when I have not upgraded my flutter. I have also tried to mock Stream using mockito but it also did not work. enter image description here

My code is as follows

flutter dependencies

flutter_bloc: ^8.0.1
mockito: ^5.1.0

my_cubit.dart

class MyCubit extends Cubit<int> {
  MyCubit() : super(0);

  void increment() {
    emit(state + 1);
  }

  void decrement() {
    emit(state - 1);
  }
}

main.dart

void main() {
  MyCubit myCubit = MyCubit();
  runApp(MyAppParent(myCubit));
}

class MyAppParent extends StatelessWidget {
  MyAppParent(this.myCubit);

  MyCubit myCubit;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('test'),
        ),
        body: BlocProvider<MyCubit>(
          create: (_) => myCubit,
          child: MyApp(),
        ),
      ),
    );
  }
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    MyCubit myCubit = context.read<MyCubit>();
    return Column(
      children: [
        BlocBuilder<MyCubit, int>(bloc: myCubit, builder: (BuildContext context, int count) {
            return Text('$count');
        }),
        TextButton(
          onPressed: () {
            myCubit.increment();
          },
          child: const Text('Increment'),
        ),
        TextButton(
          onPressed: () {
            myCubit.decrement();
          },
          child: const Text('Decrement'),
        )
      ],
    );
  }
}

widget_test.dart

class MockedMyCubit extends Mock implements MyCubit {}

void main() {
  testWidgets('Testing', (WidgetTester tester) async {
    MyCubit myCubit = MockMyCubit(); //fake object is not working, throwing exception
    // when(myCubit.stream).thenAnswer((_)  => StreamController<int>.broadcast().stream);

    // MyCubit myCubit = MyCubit(); //real object working fine
    await tester.pumpWidget(MyAppParent(myCubit));

    Finder finderCount = find.text('0');
    expect(finderCount, findsOneWidget);
    Finder finderIncrement = find.text('Increment');
    Finder finderDecrement = find.text('Decrement');

    await tester.tap(finderIncrement);
    await tester.pump();
    Finder finderCount1 = find.text('1');
    expect(finderCount1, findsOneWidget);

    await tester.tap(finderDecrement);
    await tester.pump();
    Finder finderCount0 = find.text('0');
    expect(finderCount0, findsOneWidget);
  });
}


Solution 1:[1]

I believe this is happening because the mock cubit needs to be told an initial state value. Try this after you instantiate the cubit:

when(() => myCubit.state).thenReturn(0);

Solution 2:[2]

You can use a library called mocktail.

The state type of MockedMyCubit should be provided by extending MockCubit<int> as follows:

import 'package:mocktail/mocktail.dart';

class MockedMyCubit extends MockCubit<int> implements MyCubit {}

Remember to define how it may behave using when and whenListen.

Issue discussion

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew
Solution 2 blaffie