-
-
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
@Captor
test parameters don't work with primitive type arguments
#3229
Comments
KierannCode
changed the title
@Captor test parameters don't work with primitive type arguments
Jan 8, 2024
@Captor
test parameters don't work with primitive type arguments
You can use the |
youssef3wi
added a commit
to youssef3wi/mockito
that referenced
this issue
Feb 1, 2024
8 tasks
youssef3wi
added a commit
to youssef3wi/mockito
that referenced
this issue
Feb 2, 2024
youssef3wi
added a commit
to youssef3wi/mockito
that referenced
this issue
Feb 2, 2024
youssef3wi
added a commit
to youssef3wi/mockito
that referenced
this issue
Feb 2, 2024
TimvdLippe
pushed a commit
that referenced
this issue
Feb 3, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's the first time I'm actually contributing to any public project.
I apologize if I'm missing important informations about this issue.
Description of the error :
I'm using java 21 and the dependency org.mockito:mockito-junit-jupiter version 5.8.0
The issue occurs when using an
ArgumentCaptor
as a test method parameter annotated with@Captor
, if it is used to capture the argument of a method that takes a primitive type, it throws aNullPointerException
trying to unbox anull
value to the primitive type.This only occurs when using
@Captor
on a parameter. It works fine on a field or as a localArgumentCaptor
.The
@Captor
parameters also work as expected with non-primitive types (Well kinda, I'll go back to this later)Reproduction :
Here's the sample code to reproduce the error, followed by the corresponding stacktrace (I had to order the test because the occurence of the error causes all other tests to fail somehow) :
Unsurprisingly, the first two tests pass, but the third fails with the following stacktrace :
Wait, there's more, and my proposition of solution
I investigated the issue using the debugger and noticed pretty quickly what the problem was. The "capture type" was always set to
Object
, no matter what the generic type inside theArgumentCaptor
was (even non primitive). It's not the case for local and field captors, which have the expected "capture type". Knowing that, since thecapture()
method returndefaultValue(clazz)
, it always returnnull
. It still works somehow for non primitive method parameters, butnull
cannot be passed as a primitive method parameter, hence theNullPointerException
about the unboxing. So I investigated a bit more in theCaptorParameterResolver
class, and I think I know what's wrong. It uses theCaptorAnnotationProcessor.process
method, which usesnew GenericMaster().getGenericType(parameter)
to determine the generic type to capture.However, this leads to the invocation of the method
GenericMaster.getGenericType(Parameter parameter)
.This method uses
Parameter.getType()
to look for the type parameter, but that actually returns the raw class of the parameter, so the informations about type parameters are lost. In the methodGenericMaster.getGenericType(Field field)
used for fields, it usesField.getGenericType()
that actually return the type with type parameters. The equivalent of this method for Parameter isParameter.getParameterizedType()
.I think this is clearly a bug, and the fix seems pretty obvious to me :
In GenericMaster.java, line 30, replace
return getaClass(parameter.getType());
by
return getaClass(parameter.getParameterizedType());
I tested it with a workaround and it works as intended (as a proof of concept only)
Furthermore, it seems like this method (and all the captor parameter resolving circuit) isn't used anywhere else, so there's very little impact to expect from this fix.
The text was updated successfully, but these errors were encountered: