-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Fixes #2626 : Introduce MockSettings.mockMaker #2701
Conversation
I was able to run |
Codecov Report
@@ Coverage Diff @@
## main #2701 +/- ##
============================================
+ Coverage 86.04% 86.18% +0.13%
- Complexity 2815 2828 +13
============================================
Files 320 320
Lines 8529 8583 +54
Branches 1053 1060 +7
============================================
+ Hits 7339 7397 +58
+ Misses 913 905 -8
- Partials 277 281 +4
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! Definitely getting there, but left some initial review comments. Looking forward to this feature 😄
...ain/java/org/mockito/internal/stubbing/defaultanswers/RetrieveGenericsForDefaultAnswers.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are my changes. I hope I am able to respond faster the next time. We also have to think about the API. Should the user be able to provide arbitrary mock makers (as right now in the PR), or should we make it more specific to the upcoming migration.
Generic
withSettings().mockMaker(SomeMockMaker.class)
In this case, we need to expose the internal mock makers somehow, or provide separate methods in the settings interface.
Specific
withSettings().useSubclassMockMaker()
Keeping the interface more specific to the upcoming migration would make it easier to deprecate and remove the feature in the future if it causes maintenance problems for some reason.
src/main/java/org/mockito/internal/configuration/SpyAnnotationEngine.java
Outdated
Show resolved
Hide resolved
src/main/java/org/mockito/internal/util/reflection/FieldInitializer.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, yes this is looking a lot better. Very happy about this design choice, this looks natural for loading custom mockmakers as well. I like your solution of MockMakers.java
, since these strings were kinda magic at first and now we have a proper dynamic API for them as well.
Some last minor changes, but the overall design LGTM now.
We should also include a nice mention in our main Javadoc. Can you add section 53 to Mockito.java
? Something along the lines of:
* <h3 id="53">53. <a class="meaningful_link" href="#mock_annotation_mockmaker" name="mock_annotation_mockmaker">
* New mockmaker attribute for @Mock annotation and <code>MockSettings.mockMaker(String)</code> methods (Since 4.6.0)</a></h3>
*
* You can now customize the mockmaker for a single mock, either using `@Mock` annotation mockmaker attribute or
* using `MockSettings.mockMaker(String)`. This can be useful for migrating to a different mockmakers
* (which we are planning to do in Mockito 5 with https://github.com/mockito/mockito/issues/2589).
*
* <pre class="code"><code class="java">
* @Mock(mockMaker = MockMakers.INLINE)
* Foo mock;
* // using MockSettings.withSettings()
* Foo mock = Mockito.mock(Foo.class, withSettings().mockMaker(MockMakers.SUBCLASS));
* </code></pre>
Also, make sure you add a link to section 53 at the top of the Javadoc section.
src/main/java/org/mockito/internal/configuration/SpyAnnotationEngine.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made some adjustments. I want to update the Javadoc later this week. There are also a few remaining TODOs I would like to discuss before merging this pull request.
src/main/java/org/mockito/internal/configuration/SpyAnnotationEngine.java
Outdated
Show resolved
Hide resolved
src/main/java/org/mockito/internal/configuration/plugins/DefaultMockitoPlugins.java
Outdated
Show resolved
Hide resolved
@TimvdLippe, I have finished my work. If you have no further suggestions, I would replace my FYI: I noticed that the following test is flaky, even on the Lines 17 to 24 in 530558a
It sometimes throws an |
@JojOatXGME Great work! The next version will be 4.8.0, so let's use that in the documentation 😄 Regarding the flaky test: if you have time to fix it, please go ahead. But for now, since we retry our tests, I think we can keep it as-is for now. Not ideal, I know... |
7ad0472
to
3d87a43
Compare
3d87a43
to
181d961
Compare
@TimvdLippe I replaced my version placeholder and rebased and squashed my commits. I completely forgot your suggestion while writing the Javadoc, so if you have any final remarks or prefer another wording, I am happy to make adjustments. Otherwise, I think there is nothing left to be done for this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woohoo! Great feature 😄
Running the final checks on CI and then I will merge.
import org.mockito.plugins.MockMaker; | ||
import org.mockitoutil.ClassLoaders; | ||
|
||
public class DeferMockMakersClassLoadingTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice test!
Released as part of 4.8.0 🎉 |
I implemented a prototype to fix #2626 as suggested in #2589 (comment). The basics doesn't seem that difficult, but I could have missed some edge cases. I mostly skipped documentation and tests for now. Furthermore, I ignored that
ByteBuddyMockMaker
andInlineByteBuddyMockMaker
are not exposed by the API, so we still have to adjust the API to allow setting the mock maker without accessing these classes.Personally, I don't need this functionality right now. However, if you think it would be a worthwhile addition, I can still try to help with that topic.
This pull request allows users of Mockito to choose different MockMakers per mock. This feature could ease the migration from one MockMaker to another in a big project.
PS: The Gradle taskspotlessApply
failed on my system with “java.lang.NoClassDefFoundError: Could not initialize class com.google.googlejavaformat.java.java14.Java14InputAstVisitor”. Probably because it couldn't findBindingPatternTree
. Not sure if it is a local problem or something with the project configuration.Checklist
including project members to get a better picture of the change
commit is meaningful and help the people that will explore a change in 2 years
Fixes #<issue number>
in the description if relevantFixes #<issue number>
if relevant