com.fitbur.mockito.internal.handler.NullResultGuardian Maven / Gradle / Ivy
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package com.fitbur.mockito.internal.handler;
import static com.fitbur.mockito.internal.util.Primitives.defaultValue;
import java.util.List;
import com.fitbur.mockito.internal.InternalMockHandler;
import com.fitbur.mockito.internal.stubbing.InvocationContainer;
import com.fitbur.mockito.invocation.Invocation;
import com.fitbur.mockito.mock.MockCreationSettings;
import com.fitbur.mockito.stubbing.Answer;
/**
* Protects the results from delegate MockHandler. Makes sure the results are valid.
*
* by Szczepan Faber, created at: 5/22/12
*/
class NullResultGuardian implements InternalMockHandler {
private final InternalMockHandler delegate;
public NullResultGuardian(InternalMockHandler 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 void setAnswersForStubbing(List> answers) {
delegate.setAnswersForStubbing(answers);
}
@Override
public InvocationContainer getInvocationContainer() {
return delegate.getInvocationContainer();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy