/* * Copyright (c) 2007 Mockito contributors * This program is made available under the terms of the MIT License. */ package org.mockito.internal.stubbing.defaultanswers; import java.io.Serializable; import java.lang.reflect.Modifier; import org.mockito.Mockito; import org.mockito.exceptions.Reporter; import org.mockito.internal.debugging.LocationImpl; import org.mockito.invocation.Location; import org.mockito.internal.util.ObjectMethodsGuru; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; /** * Optional Answer that can be used with * {@link Mockito#mock(Class, Answer)} *

* This implementation can be helpful when working with legacy code. Unstubbed * methods often return null. If your code uses the object returned by an * unstubbed call you get a NullPointerException. This implementation of * Answer returns SmartNulls instead of nulls. * SmartNull gives nicer exception message than NPE because it points out the * line where unstubbed method was called. You just click on the stack trace. *

* ReturnsSmartNulls first tries to return ordinary return values (see * {@link ReturnsMoreEmptyValues}) then it tries to return SmartNull. If the * return type is not mockable (e.g. final) then ordinary null is returned. *

* ReturnsSmartNulls will be probably the default return values strategy in * Mockito 2.0 */ public class ReturnsSmartNulls implements Answer, Serializable { private static final long serialVersionUID = 7618312406617949441L; private final Answer delegate = new ReturnsMoreEmptyValues(); public Object answer(final InvocationOnMock invocation) throws Throwable { Object defaultReturnValue = delegate.answer(invocation); if (defaultReturnValue != null) { return defaultReturnValue; } 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; public ThrowsSmartNullPointer(InvocationOnMock unstubbedInvocation, Location location) { this.unstubbedInvocation = unstubbedInvocation; this.location = location; } public Object answer(InvocationOnMock currentInvocation) throws Throwable { if (new ObjectMethodsGuru().isToString(currentInvocation.getMethod())) { return "SmartNull returned by this unstubbed method call on a mock:\n" + unstubbedInvocation.toString(); } new Reporter().smartNullPointerException(unstubbedInvocation.toString(), location); return null; } } }