You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem is, that anyString() infers builder.request to have a String return type, which infers given to have a String methodCall parameter.
However, at runtime a ClassCastException is thrown as builder.request(anyString()) returns a HttpBuilder$MockitoMock which cannot be cast to the the String expected by given.
(I think) this is caused by RETURNS_SELFhere in the TriesToReturnSelf class. The generic method has the return type Object which is obviously assignable from mockType. This causes mockito to always undesirably return this with methods with generic return types when using RETURNS_SELF.
Is it possible for mockito to check whether the return type of the method is generic? In that case I would suggest a fix to never return this for methods with generic return types.
If you think this would be a good solution, I am happy to contribute a fix.
edit: What do you think of checking whether return type is of type Object.class, which suggests that mockito should probable return the empty value instead of this
edit 2: Added #2687 to demonstrate the issue with a test and solution proposal. Happy to update with any comments
The text was updated successfully, but these errors were encountered:
mlichtblau
changed the title
RETURNS_SELF makes partial implementation of methods with generic return type impossible
RETURNS_SELF breaks methods with generic return type
Jun 14, 2022
I'm trying to use RETURNS_SELF to mock a builder with a generic method:
It is easy enough to mock using:
However this breaks:
The problem is, that
anyString()
infersbuilder.request
to have aString
return type, which infersgiven
to have aString methodCall
parameter.However, at runtime a
ClassCastException
is thrown asbuilder.request(anyString())
returns aHttpBuilder$MockitoMock
which cannot be cast to the theString
expected bygiven
.(I think) this is caused by
RETURNS_SELF
here in theTriesToReturnSelf
class. The generic method has the return typeObject
which is obviously assignable frommockType
. This causes mockito to always undesirably returnthis
with methods with generic return types when usingRETURNS_SELF
.Is it possible for mockito to check whether the return type of the method is generic? In that case I would suggest a fix to never return
this
for methods with generic return types.If you think this would be a good solution, I am happy to contribute a fix.
edit: What do you think of checking whether return type is of type
Object.class
, which suggests that mockito should probable return the empty value instead ofthis
edit 2: Added #2687 to demonstrate the issue with a test and solution proposal. Happy to update with any comments
The text was updated successfully, but these errors were encountered: