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
Support local parameters in test methods with JUnit Jupiter #1350
Conversation
Codecov Report
@@ Coverage Diff @@
## release/2.x #1350 +/- ##
=================================================
+ Coverage 88.54% 88.62% +0.08%
+ Complexity 2391 2359 -32
=================================================
Files 296 292 -4
Lines 6005 5951 -54
Branches 727 719 -8
=================================================
- Hits 5317 5274 -43
+ Misses 507 497 -10
+ Partials 181 180 -1
Continue to review full report at Codecov.
|
I always thought that was a cool feature of the original prototype, but I can understand your rationale for omitting that feature; however, I'd make sure it's very well documented in the user guide, etc. for Mockito. Otherwise, users will likely get confused quite quickly. |
In contrast to field names -- which are always user-defined -- I would also make a special note in the documentation that the default name for a mock created for a parameter will be something very ugly like |
Regarding that latter, in the prototype we added explicit support for determining the mock name here: ... and we did not create the mock with a custom name if the user did not provide a custom name. So I'd suggest Mockito do something similar. |
I think the javadoc of
Good call, will do
We can use the type name indeed. |
Well, as the code is currently implemented, And it sounds like you wish to restrict usage to Or maybe you don't want to restrict it? But it's unclear what is supported and what isn't. I think your goal is to restrict sharing of parameter-injected mock instances between setup and test methods. So you might want to restrict mock injection for lifecycle callback methods. On the other hand, it would be a perfectly valid use case to have a mock injected into a constructor and then store it in a In summary, you'll need to think through the possible use cases and document which ones you actually support, and... the implementation should only return |
Oh I was not aware of all these cases, I thought it was only for test methods. Have to think about what we do want to support. (Maybe it all magically works already with this implementation) |
Sure.... I'd recommend you play around with it a bit to get a feel for what's possible. Then you'll be better equipped to make an informed decision. 😉 |
+1 for sharing, I think |
@TWiStErRob Please see #1348 (comment) for the full explanation |
Yes, that should be the case. But I still think the Mockito Team should provide guidance for end users demonstrating best practices when using the For example, if it were my project, I'd make sure that there are tests in place and documented examples demonstrating parameter injection for constructors, lifecycle methods, testable methods (e.g., I'm sure that either I or someone else from the JUnit team would be happy to review your work before you publish it. 😉 |
For reference junit-team/junit5#1345 is the issue that is blocking this PR for now, but the upstream issue is actively being worked on atm. |
Thank you guys for working on this! |
The JUnit issue has been resolved on their master (junit-team/junit5#1345 (comment)) so we have to wait for a new release and then I can continue working on this PR. |
@TimvdLippe, I would't hold off on working on this PR just to wait on JUnit Jupiter 5.2 to be released. That will literally take months. Plus it would prevent Mockito users from using the feature with JUnit Jupiter 5.1.x (unless we backport the convenience methods to 5.1.x). For example, in Spring Framework 5.0.5 (likely being released today), I already added custom support for the JDK bug so that Spring users aren't affected by the issue even on JUnit Jupiter 5.0.x. |
As a user, I would strongly appreciate a backport. Have to think whether we would like to maintain this code ourselves (like Spring does) or whether we simply note the limitation that this will be addressed in the next release of JUnit. |
Sure... maintaining the "workaround" is a personal decision you have to make. As for appreciating a backport... feel free to voice your opinion on the related JUnit Jupiter issue (which I reopened for that purpose). 😉 |
Per junit-team/junit5#1345 (comment) this will be backported to JUnit 5.1.1. We just have to wait till that release is out and then I can finish up this PR 🎉 |
@TimvdLippe Looks like 5.1.1 released couple of hours back :) https://search.maven.org/#artifactdetails%7Corg.junit.jupiter%7Cjunit-jupiter-engine%7C5.1.1%7Cjar |
Awesome, constructor parameter resolution has been fixed! @mockito/core PTAL |
👍 |
Will this be getting released soon? Thank you! |
This is still awaiting review of @mockito/core |
da062fe
to
4dcdaba
Compare
I finished reviewing and I'm happy to merge! There's some issue with our Travis build at the moment but we should merge soon! |
50a4cd5
to
26c17f5
Compare
I rebased and will wait for Travis feedback before merging. |
- suggested to refer to JUnit Jupiter docs on advanced features (method/constructor parameters) - reduced the docs on the JUnit Jupiter constructor parameter because it was not compelling (I was not able to understand why would I want to use it). We can improve the documentation later on, let's not block the PR. I pointed to JUnit Jupiter docs for more info.
26c17f5
to
4c8c909
Compare
Note that this implementation differs from the prototype implementation of the JUnitTeam (https://github.com/junit-team/junit5-samples/blob/7bf40178345d5ca837579c8ddb8c025401a98788/junit5-mockito-extension/src/main/java/com/example/mockito/MockitoExtension.java#L41-L73). Instead, it will do not do any parameter resolution between test methods.
Fixes #1348