Class ReturnsSmartNulls

  extended by org.mockito.internal.stubbing.defaultanswers.ReturnsSmartNulls
All Implemented Interfaces:

public class ReturnsSmartNulls
extends java.lang.Object
implements Answer<java.lang.Object>

Optional Answer that can be used with 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 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

Constructor Summary
Method Summary
 java.lang.Object answer(InvocationOnMock invocation)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public ReturnsSmartNulls()
Method Detail


public java.lang.Object answer(InvocationOnMock invocation)
                        throws java.lang.Throwable
Specified by:
answer in interface Answer<java.lang.Object>
invocation - the invocation on the mock.
the value to be returned
java.lang.Throwable - the throwable to be thrown