Skip to content

Commit

Permalink
Fixes mockito#1898 : Return mock name from toString method for deep s…
Browse files Browse the repository at this point in the history
…tub mocks (mockito#1942)
  • Loading branch information
akluball authored and epeee committed Jun 22, 2020
1 parent 69db816 commit ddfe44e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/mockitousage/stubbing/DeepStubbingTest.java
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ddfe44e

Please sign in to comment.