-
-
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
Varargs methods cause ClassCastException
in AnswerFunctionalInterfaces
#2644
Comments
I've had a bit of a dig and it looks like a bug to me. |
big-andy-coates
added a commit
to big-andy-coates/mockito
that referenced
this issue
Jun 2, 2022
Fixes: mockito#2644 Fixes issues around vararg handling for the following methods in `AdditionalAnswers`: * `returnsFirstArg` * `returnsSecondArg` * `returnsLastArg` * `returnsArgAt` * `answer` * `answerVoid` These methods were not correctly handling varargs. For example, ```java doAnswer(answerVoid( (VoidAnswer2<String, Object[]>) logger::info )).when(mock) .info(any(), (Object[]) any()); mock.info("Some message with {} {} {}", "three", "parameters", ""); ``` Would previously have resulted in a `ClassCastException` being thrown from the `mock.info` call. This was because the `answerVoid` method was not taking into account that the second parameter was a varargs parameter and was attempting to pass the second actual argument `"three"`, rather than the second _raw_ argument `["three", "parameters", ""]`.
8 tasks
big-andy-coates
added a commit
to big-andy-coates/mockito
that referenced
this issue
Jun 6, 2022
Fixes: mockito#2644 Fixes issues around vararg handling for the following methods in `AdditionalAnswers`: * `returnsFirstArg` * `returnsSecondArg` * `returnsLastArg` * `returnsArgAt` * `answer` * `answerVoid` These methods were not correctly handling varargs. For example, ```java doAnswer(answerVoid( (VoidAnswer2<String, Object[]>) logger::info )).when(mock) .info(any(), (Object[]) any()); mock.info("Some message with {} {} {}", "three", "parameters", ""); ``` Would previously have resulted in a `ClassCastException` being thrown from the `mock.info` call. This was because the `answerVoid` method was not taking into account that the second parameter was a varargs parameter and was attempting to pass the second actual argument `"three"`, rather than the second _raw_ argument `["three", "parameters", ""]`.
big-andy-coates
added a commit
to big-andy-coates/mockito
that referenced
this issue
Jun 6, 2022
Fixes: mockito#2644 Fixes issues around vararg handling for the following methods in `AdditionalAnswers`: * `returnsFirstArg` * `returnsSecondArg` * `returnsLastArg` * `returnsArgAt` * `answer` * `answerVoid` These methods were not correctly handling varargs. For example, ```java doAnswer(answerVoid( (VoidAnswer2<String, Object[]>) logger::info )).when(mock) .info(any(), (Object[]) any()); mock.info("Some message with {} {} {}", "three", "parameters", ""); ``` Would previously have resulted in a `ClassCastException` being thrown from the `mock.info` call. This was because the `answerVoid` method was not taking into account that the second parameter was a varargs parameter and was attempting to pass the second actual argument `"three"`, rather than the second _raw_ argument `["three", "parameters", ""]`.
TimvdLippe
pushed a commit
that referenced
this issue
Jun 11, 2022
Fixes issues around vararg handling for the following methods in `AdditionalAnswers`: * `returnsFirstArg` * `returnsSecondArg` * `returnsLastArg` * `returnsArgAt` * `answer` * `answerVoid` These methods were not correctly handling varargs. For example, ```java doAnswer(answerVoid( (VoidAnswer2<String, Object[]>) logger::info )).when(mock) .info(any(), (Object[]) any()); mock.info("Some message with {} {} {}", "three", "parameters", ""); ``` Would previously have resulted in a `ClassCastException` being thrown from the `mock.info` call. This was because the `answerVoid` method was not taking into account that the second parameter was a varargs parameter and was attempting to pass the second actual argument `"three"`, rather than the second _raw_ argument `["three", "parameters", ""]`. Fixes #2644
Thanks for the fix @big-andy-coates & @TimvdLippe, appreciated! |
(Verified in our project that the fix indeed fixes the original problem. Thanks! 🙏) |
Semi-related issue discovered today: #2664 (comment) |
5 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
The other day, I discovered that this code does not work:
The exception details looks as follows:
The following works, but feels much less elegant:
Is this to be considered a bug or a feature? 🙂 I get the feeling that varargs (
Object[]
under the hood) parameters do not really work when used with theanswerVoid()
-based approach as described above.Full MCVE
https://github.com/perlun/mockito-slf4-logging-example
Issue checks
Note that some configuration are impossible to mock via Mockito
The text was updated successfully, but these errors were encountered: