-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Use generic type information in TypeBasedCandidateFilter to circumvent type erasure #2921
Comments
Duplicate of #1066 |
Hi @TimvdLippe , my suggestion is not about using the name parameter of the So with my suggestion, there would be no need to use the above at all or having to align field names in the first place. |
@TimvdLippe please find PR #2923 to prove the point |
Hi @TimvdLippe , I saw this in https://github.com/mockito/mockito/actions/runs/4244476122/jobs/7425248431 :
Does Mockito want to maintain support down to Android API level 26? The method in question was added in Android API level 28: https://developer.android.com/reference/java/lang/reflect/Type#getTypeName() |
Yes we need to maintain support for level 26 for our Android userbase. |
Please have a look @TimvdLippe |
I fixed formatting, could you please approve workflow @TimvdLippe ? |
@TimvdLippe it now also works for Spys and supports matching things like |
@TimvdLippe spotless check shouldn't fail anymore now, could you please start the workflow again? |
@TimvdLippe PR build #2923 was successful, is there anything that you'd miss in the PR? |
Fixes #2921 Co-authored-by: Jörg von Frantzius <[email protected]>
Hi,
when using
@InjectMocks
in combination with multiple@Mock
fields that have the same generic type (after type erasure), this fails unless the field names in injectee and test class are equal.I stumbled over this when I added a second
Provider<T>
field to both test and injectee class, and I guess most other people doing the same will also fail here as miserably as I did (also because googling for@InjectMock
doesn't show the relevant InjectMocks documentation in the first result page ...)E.g. this test will fail because I named the test class
@Mock
fields with the suffixmock
. When deleting that name suffix, the test succeeds.I saw in the debugger that in
TypeBasedCandidateFilter.filterCandidate()
,candidateFieldToBeInjected.getGenericType().getTypeName()
yields e.g."javax.inject.Provider<java.lang.Integer>"
. So if Mockito would maintain the@Mock
field'sjava.lang.reflect.Field
along with the mock object, the generic type names could be compared.This would effectively solve the problem of type erasure for this usecase, which I'd guess is quite common ...
The text was updated successfully, but these errors were encountered: