Search in sources :

Example 1 with Factory

use of org.powermock.api.mockito.repackaged.cglib.proxy.Factory in project powermock by powermock.

the class CglibMockMaker method getHandler.

public MockHandler getHandler(Object mock) {
    if (!(mock instanceof Factory)) {
        return null;
    }
    Factory factory = (Factory) mock;
    Callback callback = factory.getCallback(0);
    if (!(callback instanceof MethodInterceptorFilter)) {
        return null;
    }
    return ((MethodInterceptorFilter) callback).getHandler();
}
Also used : Callback(org.powermock.api.mockito.repackaged.cglib.proxy.Callback) Factory(org.powermock.api.mockito.repackaged.cglib.proxy.Factory)

Example 2 with Factory

use of org.powermock.api.mockito.repackaged.cglib.proxy.Factory in project powermock by powermock.

the class ClassImposterizer method imposterise.

public <T> T imposterise(final MethodInterceptor interceptor, Class<T> mockedType, Class<?>... ancillaryTypes) {
    Class<Factory> proxyClass = null;
    Object proxyInstance = null;
    try {
        setConstructorsAccessible(mockedType, true);
        proxyClass = createProxyClass(mockedType, ancillaryTypes);
        proxyInstance = createProxy(proxyClass, interceptor);
        return mockedType.cast(proxyInstance);
    } catch (ClassCastException cce) {
        throw new MockitoException(join("ClassCastException occurred while creating the mockito proxy :", "  class to mock : " + describeClass(mockedType), "  created class : " + describeClass(proxyClass), "  proxy instance class : " + describeClass(proxyInstance), "  instance creation by : " + instantiator.getClass().getSimpleName(), "", "You might experience classloading issues, disabling the Objenesis cache *might* help (see MockitoConfiguration)"), cce);
    } finally {
        setConstructorsAccessible(mockedType, false);
    }
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) Factory(org.powermock.api.mockito.repackaged.cglib.proxy.Factory)

Example 3 with Factory

use of org.powermock.api.mockito.repackaged.cglib.proxy.Factory in project powermock by powermock.

the class ClassImposterizer method createProxy.

private Object createProxy(Class<Factory> proxyClass, final MethodInterceptor interceptor) {
    Factory proxy;
    try {
        proxy = instantiator.newInstance(proxyClass);
    } catch (InstantiationException e) {
        throw new MockitoException("Unable to create mock instance of type '" + proxyClass.getSuperclass().getSimpleName() + "'", e);
    }
    proxy.setCallbacks(new Callback[] { interceptor, SerializableNoOp.SERIALIZABLE_INSTANCE });
    return proxy;
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) Factory(org.powermock.api.mockito.repackaged.cglib.proxy.Factory) InstantiationException(org.mockito.internal.creation.instance.InstantiationException)

Aggregations

Factory (org.powermock.api.mockito.repackaged.cglib.proxy.Factory)3 MockitoException (org.mockito.exceptions.base.MockitoException)2 InstantiationException (org.mockito.internal.creation.instance.InstantiationException)1 Callback (org.powermock.api.mockito.repackaged.cglib.proxy.Callback)1