8000 Better exception when mock is invoked after expectations are verified. by dvgica · Pull Request #142 · scalamock/scalamock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Better exception when mock is invoked after expectations are verified. #142

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

Merged
merged 1 commit into from
Nov 30, 2016
Merged

Better exception when mock is invoked after expectations are verified. #142

merged 1 commit into from
Nov 30, 2016

Conversation

dvgica
Copy link
@dvgica dvgica commented Jun 3, 2016

In several instances during development, we have encountered situations where a NPE is thrown unexpectedly and without explanation. After some investigation it seems that it's happening when a mock is called after expectations have already been verified.

This can happen easily when using ScalaTest's PathSpec variants to test async code. The developer forgets to wait for their async call to complete, proceeds to call verifyExpectations, and only after the verification is the mock function invoked (at which point the mock's call log has been nullified by https://github.com/PagerDuty/ScalaMock/blob/master/core/src/main/scala/org/scalamock/MockFactoryBase.scala#L84).

This PR avoids the NPE by throwing a more appropriate exception if the callLog has been set to null.

val call = new Call(this, arguments)
callLog += call
expectationContext.handle(call) getOrElse onUnexpected(call)
if (Option(callLog).isDefined) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would better check != null or change code to prevent null because this fancy null checking is exaggerated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, changed to != null.

I would love to see the nulls removed in favour of something more idiomatic, but not sure I want to tackle that at the moment.

@xavier-fernandez
Copy link

👍

@barkhorn barkhorn self-assigned this Nov 20, 2016
@barkhorn barkhorn removed the triage label Nov 30, 2016
@barkhorn barkhorn added this to the v3.4.2 milestone Nov 30, 2016
@barkhorn barkhorn merged commit d862ac5 into scalamock:master Nov 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0