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

Make throwOnMissingStub the default (eventually) #154

Open
matanlurey opened this issue Jul 11, 2018 · 7 comments
Open

Make throwOnMissingStub the default (eventually) #154

matanlurey opened this issue Jul 11, 2018 · 7 comments

Comments

@matanlurey
Copy link
Contributor

This is a candidate for Mockito 4, and entirely opt-in before Mockito 4.

Rationale: The following currently "just works" in Mockito 3:

class Cat {
  List<String> toys;
}

class MockCat extends Mock implements Cat {}

void main() {
  var cat = new MockCat();
  cat.toys;
}

This is problematic:

  • The default behavior for unimplemented methods returns null, instead of NSM.
  • If users add new APIs to a class they often forget to add when(...) cases for them.
  • If non-nullability ever becomes available, this is incorrect behavior.

My suggestion is to start with a global opt-in/out flag, i.e.:

void main() {
  // Any instance of `Mock` now throws instead of returning null.
  Mock.throwOnMissingStub = true;
}
@srawlins
Copy link
Member

+1 I like it. I thought we implemented something like this before and it was rolled back...

@srawlins
Copy link
Member

srawlins commented Jul 11, 2018

This could technically land in Mockito 3, as we haven't released it yet, but I think we're too busy at the moment to execute the cleanup that this would require 😄 .

@TedSander
Copy link
Contributor

If it is a flag as long as we leave the default as not throwing we should be able to land in 3 right?

@srawlins
Copy link
Member

srawlins commented Nov 2, 2018

Whoops this fell off my radar and Mockito 3 landed. I say we add the feature in 3.x, default to not-throwing, and flip the default Mockito 4 or greater.

@srawlins
Copy link
Member

Just checked, since this would be a good change for NNBD, and about 1/3 of all internal tests fail when flipping the default.

@tjarvstrand
Copy link

Hi,

Is there currently any way to globally opt-in to this behavior?

@srawlins
Copy link
Member

srawlins commented Dec 9, 2021

This is the default when using @GenerateMocks to generate mocks; otherwise the default has not changed. I think we can definitely do this in 2022 though (possibly much code to touch inside Google which relies on the current default).

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

4 participants