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; }
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); } }