Skip to content
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

Fixes #1898 : Return mock name from toString method for deep stub mocks #1942

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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