org.mockito.internal.handler.NullResultGuardian Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-core Show documentation
Show all versions of mockito-core Show documentation
Mockito mock objects library core API and implementation
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.handler;
import static org.mockito.internal.util.Primitives.defaultValue;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.InvocationContainer;
import org.mockito.invocation.MockHandler;
import org.mockito.mock.MockCreationSettings;
/**
* Protects the results from delegate MockHandler. Makes sure the results are valid.
*
* by Szczepan Faber, created at: 5/22/12
*/
class NullResultGuardian implements MockHandler {
private final MockHandler delegate;
public NullResultGuardian(MockHandler delegate) {
this.delegate = delegate;
}
@Override
public Object handle(Invocation invocation) throws Throwable {
Object result = delegate.handle(invocation);
Class returnType = invocation.getMethod().getReturnType();
if (result == null && returnType.isPrimitive()) {
// primitive values cannot be null
return defaultValue(returnType);
}
return result;
}
@Override
public MockCreationSettings getMockSettings() {
return delegate.getMockSettings();
}
@Override
public InvocationContainer getInvocationContainer() {
return delegate.getInvocationContainer();
}
}