-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fixes issue #1917 #3154
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
base: main
Are you sure you want to change the base?
Fixes issue #1917 #3154
Conversation
I love that I stumbled upon this issue today, and it was raised in April 2020 and someone has a fix for it 4 days ago. I have a Test that fails with 4 Typed objects, but I was hoping Mockito would use the field names to inject the correct instances, given type erasure. I tried with @mock(name="myObjectOfStringType") and @mock(name="myObjectOfListType") etc Instead, it seems to randomly inject 1 of the mocks to all 4 objects in my class. Would very much appreciate this fix going in. |
final Object[] args = argResolver.resolveTypeInstances(constructor.getParameterTypes()); | ||
final Object[] args = argResolver.resolveTypeInstancesGeneric( | ||
hashmap, constructor.getGenericParameterTypes()); | ||
//final Object[] args = argResolver.resolveTypeInstances(constructor.getParameterTypes()); |
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.
You are removing the only usage of resolveTypeInstances
afaik, so instead of introducing a new method, let's fix the existing method.
HashMap<String, Type> hashmap = new HashMap<>(); | ||
|
||
for (Field field : fields) { | ||
hashmap.put(field.getName(), field.getGenericType()); |
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.
Do we really need this hashmap? You are comparing objects with strings in the hashmap in the implementation, but that can lead to false-positives. I am also not sure why we would do that lookup there, as we can also pass in all the fields of the testclass into the lookup and perform the injection there.
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.
Hi thanks for the feedback! Definitely do agree that the hashmap is quite redundant but there's an issue storing mocks as a set of Objects, as this causes type erasure. I'll try to remove this hashmap in the next commit as well as store mocks as a set of Types instead of Objects.
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #3154 +/- ##
=============================================
- Coverage 85.51% 12.41% -73.11%
+ Complexity 2908 424 -2484
=============================================
Files 333 333
Lines 8852 8879 +27
Branches 1095 1104 +9
=============================================
- Hits 7570 1102 -6468
- Misses 994 7566 +6572
+ Partials 288 211 -77
☔ View full report in Codecov by Sentry. |
Fixes #1917 where Mockito cannot differentiate injection between classes with different type parameters. Instead of passing
constructor.getParameterTypes() to argResolver it passes constructor.getGenericParameterTypes() in order to properly compare the Type Parameters of the objects that are being injected. Also added tests to ensure Mockito is injecting mocks as intended and corrected tests that used the original resolveTypeInstances() vs the new resolveTypeInstancesGeneric()
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