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

Improve error message if initializer fails. #2098

Merged
merged 2 commits into from Nov 15, 2020
Merged

Conversation

raphw
Copy link
Member

@raphw raphw commented Nov 10, 2020

Non-initializable classes can never be mocked, an error message should help guide on these issues.

Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

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

LGTM but you need to run Spotless locally.

@raphw
Copy link
Member Author

raphw commented Nov 10, 2020

Indeed, just updated the PR.

join(
"Cannot instrument class that could not be initialized.",
"",
"A class that is not initializable would always fail during instrumentation.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be omitted because it is slightly reundant? The first sentence already mentions that a class which cannot be initialized cannot be instrumented.

"Cannot instrument class that could not be initialized.",
"",
"A class that is not initializable would always fail during instrumentation.",
"Static initializers are never mocked by Mockito to avoid permanent disintegration of classes."),
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it maybe good to prefix this with "Note: " / "Hint: " to indicate that this is only a hint, but most likely not something the user has to consider?
Or maybe omit it? Because this is a general hint and not directly related to the exception, unless a user tries to use Mockito to prevent an initialization error (which is rather unlikely).

Also "permanent disintegration of classes" might be too technical / unclear for the user?

"A class that is not initializable would always fail during instrumentation.",
"Static initializers are never mocked by Mockito to avoid permanent disintegration of classes."),
e.getException());
} catch (Throwable ignored) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this actually needed? Because type already exists so a ClassNotFoundException cannot be thrown (except for JDK 15 hidden classes?). Therefore the only possible error is LinkageError, and should that actually be caught?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, one never knows what JVMs do, especially if not HotSpot. Retaining the original exception is always a good idea.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah sorry, I think you have misunderstood me. I meant that it might be good to remove this catch block which silently discards any Throwable, so the caller has to deal with them instead. Or only discard the relevant exceptions, i.e. ClassNotFoundException for hidden classes?
This catch block was likely also the reason why the previous error (which this pull request improves) was so cryptic / did not contain the relevant information.

And sorry in case my previous review comments were too intrusive.

@codecov-io
Copy link

codecov-io commented Nov 15, 2020

Codecov Report

Merging #2098 (f219efb) into release/3.x (c596aef) will decrease coverage by 0.07%.
The diff coverage is 60.86%.

Impacted file tree graph

@@                Coverage Diff                @@
##             release/3.x    #2098      +/-   ##
=================================================
- Coverage          84.80%   84.73%   -0.08%     
- Complexity          2716     2717       +1     
=================================================
  Files                325      325              
  Lines               8266     8278      +12     
  Branches             988      989       +1     
=================================================
+ Hits                7010     7014       +4     
- Misses               982      993      +11     
+ Partials             274      271       -3     
Impacted Files Coverage Δ Complexity Δ
...al/creation/bytebuddy/InlineBytecodeGenerator.java 89.04% <60.86%> (-3.72%) 39.00 <0.00> (+1.00) ⬇️
.../internal/creation/bytebuddy/MockMethodAdvice.java 77.61% <0.00%> (-0.30%) 23.00% <0.00%> (ø%)
...to/internal/util/concurrent/WeakConcurrentMap.java 41.48% <0.00%> (+2.12%) 11.00% <0.00%> (ø%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c596aef...de81334. Read the comment docs.

…tecodeGenerator.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
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.

None yet

4 participants