1/* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5package org.mockito.internal.stubbing.defaultanswers; 6 7import java.io.Serializable; 8import java.lang.reflect.Modifier; 9 10import org.mockito.Mockito; 11import org.mockito.exceptions.Reporter; 12import org.mockito.internal.debugging.LocationImpl; 13import org.mockito.invocation.Location; 14import org.mockito.internal.util.ObjectMethodsGuru; 15import org.mockito.invocation.InvocationOnMock; 16import org.mockito.stubbing.Answer; 17 18/** 19 * Optional Answer that can be used with 20 * {@link Mockito#mock(Class, Answer)} 21 * <p> 22 * This implementation can be helpful when working with legacy code. Unstubbed 23 * methods often return null. If your code uses the object returned by an 24 * unstubbed call you get a NullPointerException. This implementation of 25 * Answer returns SmartNulls instead of nulls. 26 * SmartNull gives nicer exception message than NPE because it points out the 27 * line where unstubbed method was called. You just click on the stack trace. 28 * <p> 29 * ReturnsSmartNulls first tries to return ordinary return values (see 30 * {@link ReturnsMoreEmptyValues}) then it tries to return SmartNull. If the 31 * return type is not mockable (e.g. final) then ordinary null is returned. 32 * <p> 33 * ReturnsSmartNulls will be probably the default return values strategy in 34 * Mockito 2.0 35 */ 36public class ReturnsSmartNulls implements Answer<Object>, Serializable { 37 38 private static final long serialVersionUID = 7618312406617949441L; 39 40 private final Answer<Object> delegate = new ReturnsMoreEmptyValues(); 41 42 public Object answer(final InvocationOnMock invocation) throws Throwable { 43 Object defaultReturnValue = delegate.answer(invocation); 44 if (defaultReturnValue != null) { 45 return defaultReturnValue; 46 } 47 Class<?> type = invocation.getMethod().getReturnType(); 48 if (!type.isPrimitive() && !Modifier.isFinal(type.getModifiers())) { 49 final Location location = new LocationImpl(); 50 return Mockito.mock(type, new ThrowsSmartNullPointer(invocation, location)); 51 } 52 return null; 53 } 54 55 private static class ThrowsSmartNullPointer implements Answer { 56 private final InvocationOnMock unstubbedInvocation; 57 private final Location location; 58 59 public ThrowsSmartNullPointer(InvocationOnMock unstubbedInvocation, Location location) { 60 this.unstubbedInvocation = unstubbedInvocation; 61 this.location = location; 62 } 63 64 public Object answer(InvocationOnMock currentInvocation) throws Throwable { 65 if (new ObjectMethodsGuru().isToString(currentInvocation.getMethod())) { 66 return "SmartNull returned by this unstubbed method call on a mock:\n" + 67 unstubbedInvocation.toString(); 68 } 69 70 new Reporter().smartNullPointerException(unstubbedInvocation.toString(), location); 71 return null; 72 } 73 } 74} 75