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?
Asked
Active
Viewed 1,627 times
9
HerrJohr
- 419
- 3
- 8
-
Felix Angelov (the author of `flutter_bloc`) made a dart package named `mocktail`, which uses most of mockito's API, but without the need to generate mock classes. https://pub.dev/packages/mocktail – Tim Brückner Jul 06 '21 at 20:13
2 Answers
14
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
sourceswhich should have this Builder applied. Seesourcesconfiguration 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
includeandexclude. Any file which matches any glob inincludeand no globs inexcludeis considered a source of the target. Whenincludeis omitted every file is considered a match.
HerrJohr
- 419
- 3
- 8
0
To extend on HerrJohr's answer:
targets:
$default:
sources:
include:
- $package$
- lib/$lib$
- lib/**
- test/**
- integration_test/**
builders:
mockito|mockBuilder:
generate_for:
include:
- test/**
- integration_test/**
See more:
- Default
sources- build/build_runner_core/lib/src/generate /options.dart - "How can I include additional sources in my build?" from build/docs/faq.md
- Default
generate_for- mockito/build.yaml
Hrishikesh Kadam
- 35,376
- 3
- 26
- 36