Search in sources :

Example 1 with LenientCopyTool

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);
}
Also used : ReturnsEmptyValues(org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues) Instantiator(org.mockito.internal.creation.instance.Instantiator) LenientCopyTool(org.mockito.internal.util.reflection.LenientCopyTool)

Example 2 with LenientCopyTool

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;
}
Also used : MockHandlerFactory.createMockHandler(org.mockito.internal.handler.MockHandlerFactory.createMockHandler) InternalMockHandler(org.mockito.internal.InternalMockHandler) MockHandler(org.mockito.invocation.MockHandler) LenientCopyTool(org.mockito.internal.util.reflection.LenientCopyTool)

Example 3 with LenientCopyTool

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;
}
Also used : ClassReplicaCreator(org.powermock.core.ClassReplicaCreator) LenientCopyTool(org.mockito.internal.util.reflection.LenientCopyTool)

Aggregations

LenientCopyTool (org.mockito.internal.util.reflection.LenientCopyTool)3 InternalMockHandler (org.mockito.internal.InternalMockHandler)1 Instantiator (org.mockito.internal.creation.instance.Instantiator)1 MockHandlerFactory.createMockHandler (org.mockito.internal.handler.MockHandlerFactory.createMockHandler)1 ReturnsEmptyValues (org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues)1 MockHandler (org.mockito.invocation.MockHandler)1 ClassReplicaCreator (org.powermock.core.ClassReplicaCreator)1