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

Mockito fails with JVM internal error when mocking class clinit does not find a transitive dependency #3499

Merged
merged 1 commit into from
Dec 3, 2024

Conversation

AndreasTu
Copy link
Contributor

Mockito now throws Errors occurred during class initialization to callers instead of swallowing it, and the JVM reporting an obscure exception.

Fixes #3498

Checklist

  • Read the contributing guide
  • PR should be motivated, i.e. what does it fix, why, and if relevant how
  • If possible / relevant include an example in the description, that could help all readers
    including project members to get a better picture of the change
  • Avoid other runtime dependencies
  • Meaningful commit history ; intention is important please rebase your commit history so that each
    commit is meaningful and help the people that will explore a change in 2 years
  • The pull request follows coding style
  • Mention Fixes #<issue number> in the description if relevant
  • At least one commit should mention Fixes #<issue number> if relevant

@codecov-commenter
Copy link

codecov-commenter commented Nov 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.68%. Comparing base (582e3fa) to head (cd0de3a).
Report is 11 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3499      +/-   ##
============================================
+ Coverage     85.59%   85.68%   +0.09%     
- Complexity     2952     2954       +2     
============================================
  Files           341      341              
  Lines          8967     8969       +2     
  Branches       1114     1115       +1     
============================================
+ Hits           7675     7685      +10     
+ Misses         1007      997      -10     
- Partials        285      287       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@AndreasTu AndreasTu changed the title Mockito fails with JVM internal error when mocking class where clinit does not find a transitive dependency Mockito fails with JVM internal error when mocking class clinit does not find a transitive dependency Nov 6, 2024
@AndreasTu AndreasTu force-pushed the MockInitBug branch 3 times, most recently from ddf7706 to 1adebc8 Compare November 6, 2024 23:12
… does not find a transitive dependency

Mockito now throws Errors occurred during class initialization to callers
instead of swallowing it, and the JVM reporting an obscure exception.

Fixes mockito#3498
@TimvdLippe
Copy link
Contributor

@raphw Can you please review this PR?

Copy link
Member

@raphw

Choose a reason for hiding this comment

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

I was under the impression that an exception within an initializer is always wrapped in an ExceptionInInitializerError. But this might not be the case? Could you elaborate and maybe comment in the code what the JVM specifies in such a case? If I was not aware of this, I would now read the code and wonder if it is redundant.

@AndreasTu
Copy link
Contributor Author

I was under the impression that an exception within an initializer is always wrapped in an ExceptionInInitializerError.

The JVM does this for Exceptions but not for Errors, so if a dependency is missing, like NoClassDefFoundError you do not get an ExceptionInInitializerError.

Also the JVM 21 -Spec writes this:

  1. Otherwise, the initializers must have completed abruptly by throwing some exception E. If the class of E is not Error or one of its subclasses, then create a new instance of the class ExceptionInInitializerError, with E as the argument, and use this object in place of E in the following step.

@TimvdLippe TimvdLippe merged commit e2c56cc into mockito:main Dec 3, 2024
18 checks passed
@AndreasTu AndreasTu deleted the MockInitBug branch December 3, 2024 06:06
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.

Mockito fails with JVM internal error when mocking class clinit does not find a transitive dependency
4 participants