1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.handler; 6 7 import org.mockito.internal.InternalMockHandler; 8 import org.mockito.internal.progress.HandyReturnValues; 9 import org.mockito.internal.stubbing.InvocationContainer; 10 import org.mockito.invocation.Invocation; 11 import org.mockito.mock.MockCreationSettings; 12 import org.mockito.stubbing.VoidMethodStubbable; 13 14 import java.util.List; 15 16 /** 17 * Protects the results from delegate MockHandler. Makes sure the results are valid. 18 * 19 * by Szczepan Faber, created at: 5/22/12 20 */ 21 class NullResultGuardian implements InternalMockHandler { 22 private final InternalMockHandler delegate; 23 24 public NullResultGuardian(InternalMockHandler delegate) { 25 this.delegate = delegate; 26 } 27 28 public Object handle(Invocation invocation) throws Throwable { 29 Object result = delegate.handle(invocation); 30 Class<?> returnType = invocation.getMethod().getReturnType(); 31 if(result == null && returnType.isPrimitive()) { 32 //primitive values cannot be null 33 return new HandyReturnValues().returnFor(returnType); 34 } else { 35 return result; 36 } 37 } 38 39 //boring delegation: 40 41 public MockCreationSettings getMockSettings() { 42 return delegate.getMockSettings(); 43 } 44 45 public VoidMethodStubbable voidMethodStubbable(Object mock) { 46 return delegate.voidMethodStubbable(mock); 47 } 48 49 public void setAnswersForStubbing(List answers) { 50 delegate.setAnswersForStubbing(answers); 51 } 52 53 public InvocationContainer getInvocationContainer() { 54 return delegate.getInvocationContainer(); 55 } 56 } 57