diff options
Diffstat (limited to 'src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsSmartNulls.java')
-rw-r--r-- | src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsSmartNulls.java | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsSmartNulls.java b/src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsSmartNulls.java index 6cf50c7..7487e89 100644 --- a/src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsSmartNulls.java +++ b/src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsSmartNulls.java @@ -8,6 +8,8 @@ import static org.mockito.internal.exceptions.Reporter.smartNullPointerException import static org.mockito.internal.util.ObjectMethodsGuru.isToStringMethod; import java.io.Serializable; +import java.lang.reflect.Modifier; + import org.mockito.Mockito; import org.mockito.internal.debugging.LocationImpl; import org.mockito.invocation.InvocationOnMock; @@ -38,34 +40,24 @@ public class ReturnsSmartNulls implements Answer<Object>, Serializable { private final Answer<Object> delegate = new ReturnsMoreEmptyValues(); - @Override public Object answer(final InvocationOnMock invocation) throws Throwable { Object defaultReturnValue = delegate.answer(invocation); - if (defaultReturnValue != null) { return defaultReturnValue; } - - return RetrieveGenericsForDefaultAnswers.returnTypeForMockWithCorrectGenerics(invocation, - new RetrieveGenericsForDefaultAnswers.AnswerCallback() { - @Override - public Object apply(Class<?> type) { - if (type == null) { - return null; - } - - return Mockito.mock(type, new ThrowsSmartNullPointer(invocation, new LocationImpl())); - } - }); + Class<?> type = invocation.getMethod().getReturnType(); + if (!type.isPrimitive() && !Modifier.isFinal(type.getModifiers())) { + final Location location = new LocationImpl(); + return Mockito.mock(type, new ThrowsSmartNullPointer(invocation, location)); + } + return null; } private static class ThrowsSmartNullPointer implements Answer { - private final InvocationOnMock unstubbedInvocation; - private final Location location; - ThrowsSmartNullPointer(InvocationOnMock unstubbedInvocation, Location location) { + public ThrowsSmartNullPointer(InvocationOnMock unstubbedInvocation, Location location) { this.unstubbedInvocation = unstubbedInvocation; this.location = location; } @@ -73,7 +65,7 @@ public class ReturnsSmartNulls implements Answer<Object>, Serializable { public Object answer(InvocationOnMock currentInvocation) throws Throwable { if (isToStringMethod(currentInvocation.getMethod())) { return "SmartNull returned by this unstubbed method call on a mock:\n" + - unstubbedInvocation.toString(); + unstubbedInvocation.toString(); } throw smartNullPointerException(unstubbedInvocation.toString(), location); |