Search in sources :

Example 1 with Instantiator

use of org.mockito.internal.creation.instance.Instantiator 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 Instantiator

use of org.mockito.internal.creation.instance.Instantiator in project mockito by mockito.

the class SubclassByteBuddyMockMaker method createMock.

@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    Class<? extends T> mockedProxyType = createMockType(settings);
    Instantiator instantiator = Plugins.getInstantiatorProvider().getInstantiator(settings);
    T mockInstance = null;
    try {
        mockInstance = instantiator.newInstance(mockedProxyType);
        MockAccess mockAccess = (MockAccess) mockInstance;
        mockAccess.setMockitoInterceptor(new MockMethodInterceptor(asInternalMockHandler(handler), settings));
        return ensureMockIsAssignableToMockedType(settings, mockInstance);
    } catch (ClassCastException cce) {
        throw new MockitoException(join("ClassCastException occurred while creating the mockito mock :", "  class to mock : " + describeClass(settings.getTypeToMock()), "  created class : " + describeClass(mockedProxyType), "  proxy instance class : " + describeClass(mockInstance), "  instance creation by : " + instantiator.getClass().getSimpleName(), "", "You might experience classloading issues, please ask the mockito mailing-list.", ""), cce);
    } catch (org.mockito.internal.creation.instance.InstantiationException e) {
        throw new MockitoException("Unable to create mock instance of type '" + mockedProxyType.getSuperclass().getSimpleName() + "'", e);
    }
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) Instantiator(org.mockito.internal.creation.instance.Instantiator)

Example 3 with Instantiator

use of org.mockito.internal.creation.instance.Instantiator in project mockito by mockito.

the class InlineByteBuddyMockMaker method createMock.

@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    Class<? extends T> type = createMockType(settings);
    Instantiator instantiator = Plugins.getInstantiatorProvider().getInstantiator(settings);
    try {
        T instance = instantiator.newInstance(type);
        MockMethodInterceptor mockMethodInterceptor = new MockMethodInterceptor(asInternalMockHandler(handler), settings);
        mocks.put(instance, mockMethodInterceptor);
        if (instance instanceof MockAccess) {
            ((MockAccess) instance).setMockitoInterceptor(mockMethodInterceptor);
        }
        return instance;
    } catch (org.mockito.internal.creation.instance.InstantiationException e) {
        throw new MockitoException("Unable to create mock instance of type '" + type.getSimpleName() + "'", e);
    }
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) Instantiator(org.mockito.internal.creation.instance.Instantiator)

Aggregations

Instantiator (org.mockito.internal.creation.instance.Instantiator)3 MockitoException (org.mockito.exceptions.base.MockitoException)2 ReturnsEmptyValues (org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues)1 LenientCopyTool (org.mockito.internal.util.reflection.LenientCopyTool)1