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

All of Mock's fields should be final #201

Open
srawlins opened this issue Jul 16, 2019 · 5 comments
Open

All of Mock's fields should be final #201

srawlins opened this issue Jul 16, 2019 · 5 comments
Labels
P2 A bug or feature request we're likely to work on S2 type-enhancement A request for a change that isn't a bug

Comments

@srawlins
Copy link
Member

#200 Really just inspired in me the idea that Mock should only have final fields; i.e. it should be able to satisfy the @immutable contract if needed (it should be immutable-able 😛). And looking at the existing fields, this should be possible:

  1. The top-level named API sets two private fields on Mock objects: _givenName and _givenHashCode. These two are used to override toString() and hashCode respectively. However, they are not much used, and they could be declared with standard when stubbing, or could be specified as optional arguments to the Mock constructor.

  2. The Mock class allows users to override default stub behavior via the _defaultResponse member. This has basically only two uses today:

    • By default, methods called without stubs return null. This default does not really make sense in the future NNBD world.
    • Users can call throwOnMissingStub to override this null-returning behavior with throwing behavior. We have Make throwOnMissingStub the default (eventually) #154 open to make this the default behavior.

    This behavior could 100% be overridden with an optional constructor parameter.

So all three of the non-final fields could be made final without too much ceremony; it would be a breaking change to remove the named and throwOnMissingStub APIs, but we can introduce the new APIs (constructor parameters) before bumping a major version.

@srawlins srawlins changed the title All of mock's fields should be final All of Mock's fields should be final Jul 16, 2019
@srawlins srawlins added P2 A bug or feature request we're likely to work on S2 type-enhancement A request for a change that isn't a bug labels Jul 16, 2019
@srawlins
Copy link
Member Author

CC @matanlurey @TedSander if you'd like to vote yay or nay; I'd like to implement the new constructor and mark the current APIs as deprecated, sooner rather than later, so that we can give a long time window before Mockito 5-or-so, removing them.

@TedSander
Copy link
Contributor

Seems fine to me. I don't see any obvious reason why it shouldn't be.

@Peng-Qian
Copy link

any updates? 2021 already...

@srawlins
Copy link
Member Author

srawlins commented Mar 3, 2021

No updates. This will likely go into Mockito's next major release.

@maxzod
Copy link

maxzod commented Mar 10, 2024

the Workaround for my case it was only affecting the health checks in GitHub actions , so i had to skip the generated files from the dart analyzer
in analysis_options.yaml i added

analyzer:
  exclude:
    - test/**.mocks.dart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 A bug or feature request we're likely to work on S2 type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants