Search code examples
flutterdartintegration-testingmockito-dart

Is there a way to let mockito generate mocks for integration tests in a Flutter project


Mockito generates mocks for all classes listet within the @GenerateMocks annotatio. It does so for tests in the test folder but it doesn't for tests in the integration_test folder. How do I change that?


Solution

  • TL;DR

    Add a build.yaml file with the following content to your project root folder.

    targets:
      $default:
        sources:
          - $package$
          - lib/$lib$
          - lib/**.dart
          - test/**.dart
          - integration_test/**.dart
        builders:
          mockito|mockBuilder:
            generate_for:
              - test/**.dart
              - integration_test/**.dart
    

    Explanation

    Both generate_for and sources are needed to tell the mockBuilder which files should be processed.

    The generate_for configuration is only a subset of the all the files used by the builder. But these files do not include the integration_test folder by default. To modify that, we can list the sources files manually. We have to include the default sources $package$and lib/$lib$, or we will get warnings if not.

    By including any folder - in our case 'integration_test' it will then also be available to generate_for.

    Here is the excerpt from the build_config/README.md:

    • generate_for: List of String or Map, Optional:. The subset of files within the target's sources which should have this Builder applied. See sources configuration above for how to configure this.

    and:

    • sources: List of Strings or Map, Optional. The set of files within the package which make up this target. Files are specified using glob syntax. If a List of Strings is used they are considered the 'include' globs. If a Map is used can only have the keys include and exclude. Any file which matches any glob in include and no globs in exclude is considered a source of the target. When include is omitted every file is considered a match.