Search in sources :

Example 1 with ClassReplicaCreator

use of org.powermock.core.ClassReplicaCreator in project powermock by powermock.

the class PowerMock method createReplicaType.

private static <T> Class<?> createReplicaType(Class<T> type, boolean isStatic, ConstructorArgs constructorArgs) {
    ClassReplicaCreator classReplicaCreator = new ClassReplicaCreator();
    Class<?> replicaType = null;
    if (isStatic || constructorArgs == null) {
        replicaType = classReplicaCreator.createClassReplica(type);
    } else {
        try {
            replicaType = classReplicaCreator.createInstanceReplica(constructorArgs.getConstructor().newInstance(constructorArgs.getInitArgs()));
        } catch (RuntimeException e) {
            throw e;
        } catch (InvocationTargetException e) {
            Throwable targetException = e.getTargetException();
            if (targetException instanceof RuntimeException) {
                throw (RuntimeException) targetException;
            }
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return replicaType;
}
Also used : ClassReplicaCreator(org.powermock.core.ClassReplicaCreator)

Example 2 with ClassReplicaCreator

use of org.powermock.core.ClassReplicaCreator 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

ClassReplicaCreator (org.powermock.core.ClassReplicaCreator)2 LenientCopyTool (org.mockito.internal.util.reflection.LenientCopyTool)1