Repository: mockito/mockito
Issue: RETURNS_DEEP_STUBS override a mock's toString to `null`
Labels: please contribute

This example describes the issue:

```
  @Test
  public void mocksWithDeepStubsReturnNameOnToString() {
    Object proudMock = Mockito.mock(
        Object.class,
        Mockito.withSettings()
            .name("I'm a mock!")
            .defaultAnswer(Mockito.RETURNS_DEEP_STUBS));

    // fails, toString() returns null
    assertThat(proudMock.toString()).isEqualTo("I'm a mock!");
  }
```

I'm experiencing this on 2.26.0, (looking at [ReturnsDeepStubs](https://github.com/mockito/mockito/blame/release/3.x/src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsDeepStubs.java) looks like it hasn't changed much since then).

I think toString should still return the mock's name, right now it returns a `null`.


Issue comments:

Pull request review comments:

Patch that resolved the issue:
diff --git a/src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsDeepStubs.java b/src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsDeepStubs.java
index 939b8b8f5d..a0c537fe79 100644
--- a/src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsDeepStubs.java
+++ b/src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsDeepStubs.java
@@ -52,7 +52,11 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
 
         Class<?> rawType = returnTypeGenericMetadata.rawType();
         if (!mockitoCore().isTypeMockable(rawType)) {
-            return delegate().returnValueFor(rawType);
+            if (invocation.getMethod().getReturnType().equals(rawType)) {
+                return delegate().answer(invocation);
+            } else {
+                return delegate().returnValueFor(rawType);
+            }
         }
 
         // When dealing with erased generics, we only receive the Object type as rawType. At this


Test patch:
diff --git a/src/test/java/org/mockitousage/stubbing/DeepStubbingTest.java b/src/test/java/org/mockitousage/stubbing/DeepStubbingTest.java
index 3740a28b9d..d1297a059e 100644
--- a/src/test/java/org/mockitousage/stubbing/DeepStubbingTest.java
+++ b/src/test/java/org/mockitousage/stubbing/DeepStubbingTest.java
@@ -6,6 +6,7 @@
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.fail;
@@ -22,6 +23,7 @@
 
 import org.junit.Test;
 import org.mockito.InOrder;
+import org.mockito.MockSettings;
 import org.mockito.exceptions.verification.TooManyActualInvocations;
 import org.mockitoutil.TestBase;
 
@@ -222,6 +224,20 @@ public void withPatternPrimitive() throws Exception {
         assertEquals(a, sf.createSocket("stackoverflow.com", 80).getPort());
     }
 
+    @Test
+    public void unnamed_to_string() {
+        SocketFactory sf = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
+        assertNotNull(sf.toString());
+    }
+
+    @Test
+    public void named_to_string() {
+        MockSettings settings = withSettings().name("name of mock")
+                                              .defaultAnswer(RETURNS_DEEP_STUBS);
+        SocketFactory sf = mock(SocketFactory.class, settings);
+        assertEquals("name of mock", sf.toString());
+    }
+
     Person person = mock(Person.class, RETURNS_DEEP_STUBS);
 
     @Test