Search in sources :

Example 1 with InstantiationException

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

the class JavaEightUtil method invokeNullaryFactoryMethod.

/**
     * Invokes a nullary static factory method using reflection to stay backwards-compatible with older JDKs.
     *
     * @param fqcn The fully qualified class name of the type to be produced.
     * @param methodName The name of the factory method.
     * @return the object produced.
     */
private static Object invokeNullaryFactoryMethod(final String fqcn, final String methodName) {
    try {
        final Class<?> type = Class.forName(fqcn);
        final Method method = type.getMethod(methodName);
        return method.invoke(null);
    // any exception is really unexpected since the type name has
    // already been verified
    } catch (final Exception e) {
        throw new InstantiationException(String.format("Could not create %s#%s(): %s", fqcn, methodName, e), e);
    }
}
Also used : Method(java.lang.reflect.Method) InstantiationException(org.mockito.internal.creation.instance.InstantiationException) InstantiationException(org.mockito.internal.creation.instance.InstantiationException)

Example 2 with InstantiationException

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

InstantiationException (org.mockito.internal.creation.instance.InstantiationException)2 Method (java.lang.reflect.Method)1 MockitoException (org.mockito.exceptions.base.MockitoException)1 Factory (org.powermock.api.mockito.repackaged.cglib.proxy.Factory)1