1

I am trying to use @GenerateMocks on my test method, This is my dev dependancies:

dev_dependencies:
  test: any
  flutter_test:
    sdk: flutter
  flutter_driver:
    sdk: flutter
  integration_test:
    sdk: flutter
  flutter_lints: ^1.0.4
  build_runner: ^2.1.4
  mockito: ^5.0.0
  mocktail: ^0.2.0

and I am using like this:

@GenerateMocks([CustomerPOAutocompleteState])
void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

But after running flutter pub run build_runner build nothing is generated?

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149

2 Answers2

0

As per documentation

If you need to mock your GetxController/GetxService, you should extend GetxController, and mixin it with Mock, that way

class NotificationServiceMock extends GetxService 
       with Mock implements NotificationService {}

I directly added mockito on dependencies.

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  flutter_bloc: ^8.0.1
  equatable: ^2.0.3
  dartz: ^0.10.1
  mockito: ^5.2.0 #here


dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^2.1.11

Make sure to create some methods on your abstract class, in my case it is RestaurantRepository and will be annotated like @GenerateMocks([RestaurantRepository]).

And then run

flutter pub run build_runner build

On test file

import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

@GenerateMocks([RestaurantRepository])
void main() {
  late MockRestaurantRepository mockRestaurantRepository;
//...}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
0

Don't know if this is it but i've just put the import 'my_test_file.mocks.dart'; after the @GenerateMocks annotation and worked fine.

before:

@GenerateMocks([MyClass])

after:

@GenerateMocks([MyClass])
import 'my_test_file.mocks.dart';