use of org.mockito.internal.util.reflection.LenientCopyTool in project mockito by mockito.
the class ClonesArguments method answer.
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
for (int i = 0; i < arguments.length; i++) {
Object from = arguments[i];
Instantiator instantiator = Plugins.getInstantiatorProvider().getInstantiator(null);
Object newInstance = instantiator.newInstance(from.getClass());
new LenientCopyTool().copyToRealObject(from, newInstance);
arguments[i] = newInstance;
}
return new ReturnsEmptyValues().answer(invocation);
}
use of org.mockito.internal.util.reflection.LenientCopyTool in project mockito by mockito.
the class MockUtil method createMock.
public static <T> T createMock(MockCreationSettings<T> settings) {
MockHandler mockHandler = createMockHandler(settings);
T mock = mockMaker.createMock(settings, mockHandler);
Object spiedInstance = settings.getSpiedInstance();
if (spiedInstance != null) {
new LenientCopyTool().copyToMock(spiedInstance, mock);
}
return mock;
}
use of org.mockito.internal.util.reflection.LenientCopyTool in project powermock by powermock.
the class DefaultMockCreator method createMock.
@SuppressWarnings("unchecked")
public <T> T createMock(Class<T> type, boolean isStatic, boolean isSpy, Object delegator, MockSettings mockSettings, Method... methods) {
if (type == null) {
throw new IllegalArgumentException("The class to mock cannot be null");
}
validateType(type, isStatic, isSpy);
final String mockName = toInstanceName(type, mockSettings);
MockRepository.addAfterMethodRunner(new MockitoStateCleanerRunnable());
final Class<T> typeToMock;
if (isFinalJavaSystemClass(type)) {
typeToMock = (Class<T>) new ClassReplicaCreator().createClassReplica(type);
} else {
typeToMock = type;
}
final MockData<T> mockData = createMethodInvocationControl(mockName, typeToMock, methods, isSpy, delegator, mockSettings);
T mock = mockData.getMock();
if (isFinalJavaSystemClass(type) && !isStatic) {
mock = Whitebox.newInstance(type);
DefaultFieldValueGenerator.fillWithDefaultValues(mock);
}
if (isStatic) {
MockRepository.putStaticMethodInvocationControl(type, mockData.getMethodInvocationControl());
} else {
MockRepository.putInstanceMethodInvocationControl(mock, mockData.getMethodInvocationControl());
}
if (isSpy) {
new LenientCopyTool().copyToMock(delegator, mock);
}
return mock;
}
Aggregations