mockito icon indicating copy to clipboard operation
mockito copied to clipboard

All of Mock's fields should be final

Open srawlins opened this issue 6 years ago • 5 comments

#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 #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 avatar Jul 16 '19 04:07 srawlins

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.

srawlins avatar Jul 16 '19 19:07 srawlins

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

TedSander avatar Jul 18 '19 22:07 TedSander

any updates? 2021 already...

Peng-Qian avatar Mar 03 '21 10:03 Peng-Qian

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

srawlins avatar Mar 03 '21 13:03 srawlins

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

maxzod avatar Mar 10 '24 11:03 maxzod