Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mockito:mockBuilder generator fail using MockSpec custom superclass #729

Open
oznecniV97 opened this issue Dec 21, 2023 · 5 comments
Open

Comments

@oznecniV97
Copy link

Mockito generator fail using a custom superclass that extends MockSpec inside GenerateNiceMocks annotation.

Command: dart run build_runner build
Error:

[SEVERE] mockito:mockBuilder on test/mockito_super_test.dart:
Null check operator used on a null value

The problem is related to _mockTargetFromMockSpec method inside lib/src/builder.dart file, because of superclass contains values inside (super) field.

E.g.:

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'mockito_super_test.mocks.dart';

class ExampleMockSpec<T> extends MockSpec<T> {
  const ExampleMockSpec() : super();
}

class ExampleService {
  String testMethod() {
    return "example";
  }
}

@GenerateNiceMocks([ExampleMockSpec<ExampleService>()])
void main() {
  test('exampleTest', () {
    const mockValue = "mockValue";
    final mock = MockExampleService();
    when(mock.testMethod()).thenReturn(mockValue);
    expect(mock.testMethod(), mockValue);
  });
}
@yanok
Copy link
Contributor

yanok commented Dec 21, 2023

Uh, I feel like the right thing to do here is just to make MockSpec class final. Why would anyone want to subclass it?

@oznecniV97
Copy link
Author

I need to extend MockSpec to use a default fallbackGenerators for every classes.

EG:

class GetxMockSpec<T> extends MockSpec<T> {

  const GetxMockSpec() : super(
      fallbackGenerators: const {
        #onStart: fallbackGenerator,
      }
  );

}

@yanok
Copy link
Contributor

yanok commented Dec 21, 2023

Could be a wrapper function instead.

MockSpec<T> getxMockSpec<T>() => MockSpec<T>(fallbackGenerators: {#onStart: generator});

(and I really hope we'll be able to get rid of fallback generators in the next major version).

@srawlins
Copy link
Member

Can the wrapper function be used in an annotation?

@oznecniV97
Copy link
Author

Wrapper function can't be used inside annotation:
Methods can't be invoked in constant expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants