Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright (c) 2007 Mockito contributors
  * This program is made available under the terms of the MIT License.
 package org.mockito.internal.stubbing.defaultanswers;
It's likely this implementation will be used by default by every Mockito 2.0 mock.

Currently used only by org.mockito.Mockito.RETURNS_SMART_NULLS

Current version of Mockito mocks by deafult use ReturnsEmptyValues

  • Returns appropriate primitive for primitive-returning methods
  • Returns consistent values for primitive wrapper classes (e.g. int-returning method retuns 0 and Integer-returning method returns 0, too)
  • Returns empty collection for collection-returning methods (works for most commonly used collection types)
  • Returns empty array for array-returning methods
  • Returns "" for String-returning method
  • Returns description of mock for toString() method
  • Returns null for everything else
public class ReturnsMoreEmptyValues implements Answer<Object> {
    private static final long serialVersionUID = -2816745041482698471L;
    private Answer<Objectdelegate = new ReturnsEmptyValues();
    /* (non-Javadoc)
     * @see org.mockito.stubbing.Answer#answer(org.mockito.invocation.InvocationOnMock)
    public Object answer(InvocationOnMock invocationthrows Throwable {
        Object ret = .answer(invocation);
        if (ret != null) {
            return ret;
        Class<?> returnType = invocation.getMethod().getReturnType();
        return returnValueFor(returnType);
    Object returnValueFor(Class<?> type) {
        if (type == String.class) {
            return "";
        }  else if (type.isArray()) {
            Class<?> componenetType = type.getComponentType();
            return Array.newInstance(componenetType, 0);
        return null;
New to GrepCode? Check out our FAQ X