Repository: mockito/mockito
Issue: RETURNS_SELF breaks methods with generic return type

I'm trying to use RETURNS_SELF to mock a builder with a generic method:

```Java
private static class HttpBuilder {
  private String uri;
  
  public HttpBuilder withUri(String uri) {
    this.uri = uri;
    return this
  }

  ...

  public <T> T request(T result) {
    return result; // Do typed request
  }
}
```

It is easy enough to mock using:

```Java
val builder = mock(HttpBuilder.class, RETURNS_SELF);
```

However this breaks:

```Java
given(builder.request(anyString())).willAnswer(i → "Mocked result");
```


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_SELF` [here](https://github.com/mockito/mockito/blob/6f9108b833323345bd926f10914224d3e0a11b80/src/main/java/org/mockito/internal/stubbing/defaultanswers/TriesToReturnSelf.java#L23) 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

Issue comments:

Pull request review comments:

Patch that resolved the issue:
diff --git a/src/main/java/org/mockito/internal/stubbing/defaultanswers/TriesToReturnSelf.java b/src/main/java/org/mockito/internal/stubbing/defaultanswers/TriesToReturnSelf.java
index 58a64d722e..f5643c565b 100644
--- a/src/main/java/org/mockito/internal/stubbing/defaultanswers/TriesToReturnSelf.java
+++ b/src/main/java/org/mockito/internal/stubbing/defaultanswers/TriesToReturnSelf.java
@@ -20,7 +20,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
         Object mock = invocation.getMock();
         Class<?> mockType = MockUtil.getMockHandler(mock).getMockSettings().getTypeToMock();
 
-        if (methodReturnType.isAssignableFrom(mockType)) {
+        if (methodReturnType.isAssignableFrom(mockType) && methodReturnType != Object.class) {
             return invocation.getMock();
         }
 


Test patch:
diff --git a/src/test/java/org/mockitousage/stubbing/StubbingReturnsSelfTest.java b/src/test/java/org/mockitousage/stubbing/StubbingReturnsSelfTest.java
index 4e75dfb5c6..28b9cea895 100644
--- a/src/test/java/org/mockitousage/stubbing/StubbingReturnsSelfTest.java
+++ b/src/test/java/org/mockitousage/stubbing/StubbingReturnsSelfTest.java
@@ -73,6 +73,13 @@ public void should_not_fail_when_calling_primitive_returning_method() {
         assertThat(builder.returnInt()).isEqualTo(0);
     }
 
+    @Test
+    public void should_not_fail_when_calling_method_with_generic_return_type() {
+        Builder builder = mock(Builder.class, RETURNS_SELF);
+
+        assertThat(builder.returnGeneric("Generic Result")).isEqualTo(null);
+    }
+
     @Test
     public void use_full_builder_with_terminating_method() {
         HttpBuilder builder = mock(HttpBuilder.class, RETURNS_SELF);
@@ -99,6 +106,10 @@ public void returnNothing() {}
         public int returnInt() {
             return 1;
         }
+
+        public <T> T returnGeneric(T result) {
+            return result;
+        }
     }
 
     private static class BuilderSubClass extends Builder {