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

Close mocking session on aborted Scalatest runs #213

Merged

Conversation

einholen
Copy link
Contributor

@einholen einholen commented Mar 24, 2020

Fixes #211 .

A Scalatest test case run (super.withFixture(test)) will actually throw an exception when the entire test suite must be aborted (ie initialization errors or just non-Exception errors such as StackOverflowError). In such a case, the current open mocking session is never closed, and all subsequent test suites using the same base class start to fail because Mockito does not allow creating a new session when there's an unclosed one.
We can fix it by catching all throwables, trying to close the session and rethrow.

case NonFatal(ex) =>
session.finishMocking(Some(ex))
throw ex
case t: Throwable =>
Copy link
Member

Choose a reason for hiding this comment

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

So the problem was that we were not catching the StackOverflowException and hence not closing the session appropriately?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, and NonFatal(e) wouldn't catch it either - StackOverflowError is a VirtualMachineError which is technically fatal for some reason even if it's not

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And in synchronous spec, no exceptions are caught - but some do get thrown when a suite is aborted

@ultrasecreth ultrasecreth merged commit 7872b6d into mockito:release/1.x Mar 26, 2020
@einholen einholen deleted the fix-unclosed-mocking-session branch March 26, 2020 15:18
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

Successfully merging this pull request may close these issues.

Usage of scalatest IdiomaticMockito produces "unfinished mocking session" errors
2 participants