Search in sources :

Example 26 with MockitoException

use of org.mockito.exceptions.base.MockitoException in project j2objc by google.

the class IosMockMaker method createMock.

@Override
@SuppressWarnings("unchecked")
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    Class<T> typeToMock = settings.getTypeToMock();
    @SuppressWarnings("rawtypes") Set<Class> interfacesSet = settings.getExtraInterfaces();
    Class<?>[] extraInterfaces = interfacesSet.toArray(new Class[interfacesSet.size()]);
    InvocationHandler invocationHandler = new InvocationHandlerAdapter(handler);
    if (typeToMock.isInterface()) {
        // support interfaces via java.lang.reflect.Proxy
        @SuppressWarnings("rawtypes") Class[] classesToMock = new Class[extraInterfaces.length + 1];
        classesToMock[0] = typeToMock;
        System.arraycopy(extraInterfaces, 0, classesToMock, 1, extraInterfaces.length);
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        // newProxyInstance returns the type of typeToMock
        T mock = (T) Proxy.newProxyInstance(contextClassLoader, classesToMock, invocationHandler);
        return mock;
    } else {
        try {
            Class<? extends T> proxyClass = getProxyClass(typeToMock, extraInterfaces);
            T mock = proxyClass.newInstance();
            ((ClassProxy) mock).setHandler(invocationHandler);
            return mock;
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new MockitoException("Failed to mock " + typeToMock, e);
        }
    }
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler) MockitoException(org.mockito.exceptions.base.MockitoException) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 27 with MockitoException

use of org.mockito.exceptions.base.MockitoException 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)

Example 28 with MockitoException

use of org.mockito.exceptions.base.MockitoException in project mockito by mockito.

the class InlineBytecodeGenerator method triggerRetransformation.

private <T> void triggerRetransformation(MockFeatures<T> features) {
    Set<Class<?>> types = new HashSet<Class<?>>();
    Class<?> type = features.mockedType;
    do {
        if (mocked.add(type)) {
            types.add(type);
            addInterfaces(types, type.getInterfaces());
        }
        type = type.getSuperclass();
    } while (type != null);
    if (!types.isEmpty()) {
        try {
            instrumentation.retransformClasses(types.toArray(new Class<?>[types.size()]));
        } catch (UnmodifiableClassException exception) {
            for (Class<?> failed : types) {
                mocked.remove(failed);
            }
            throw new MockitoException("Could not modify all classes " + types, exception);
        }
    }
}
Also used : UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) MockitoException(org.mockito.exceptions.base.MockitoException) HashSet(java.util.HashSet)

Example 29 with MockitoException

use of org.mockito.exceptions.base.MockitoException in project mockito by mockito.

the class SpyAnnotationEngine method process.

@Override
public void process(Class<?> context, Object testInstance) {
    Field[] fields = context.getDeclaredFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(Spy.class) && !field.isAnnotationPresent(InjectMocks.class)) {
            assertNoIncompatibleAnnotations(Spy.class, field, Mock.class, Captor.class);
            field.setAccessible(true);
            Object instance;
            try {
                instance = field.get(testInstance);
                if (MockUtil.isMock(instance)) {
                    // instance has been spied earlier
                    // for example happens when MockitoAnnotations.initMocks is called two times.
                    Mockito.reset(instance);
                } else if (instance != null) {
                    field.set(testInstance, spyInstance(field, instance));
                } else {
                    field.set(testInstance, spyNewInstance(testInstance, field));
                }
            } catch (Exception e) {
                throw new MockitoException("Unable to initialize @Spy annotated field '" + field.getName() + "'.\n" + e.getMessage(), e);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) MockitoException(org.mockito.exceptions.base.MockitoException) Spy(org.mockito.Spy) MockitoException(org.mockito.exceptions.base.MockitoException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 30 with MockitoException

use of org.mockito.exceptions.base.MockitoException in project mockito by mockito.

the class ConstructorInjection method processInjection.

public boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
    try {
        SimpleArgumentResolver simpleArgumentResolver = new SimpleArgumentResolver(mockCandidates);
        FieldInitializationReport report = new FieldInitializer(fieldOwner, field, simpleArgumentResolver).initialize();
        return report.fieldWasInitializedUsingContructorArgs();
    } catch (MockitoException e) {
        if (e.getCause() instanceof InvocationTargetException) {
            Throwable realCause = e.getCause().getCause();
            throw fieldInitialisationThrewException(field, realCause);
        }
        // other causes should be fine
        return false;
    }
}
Also used : FieldInitializationReport(org.mockito.internal.util.reflection.FieldInitializationReport) MockitoException(org.mockito.exceptions.base.MockitoException) FieldInitializer(org.mockito.internal.util.reflection.FieldInitializer) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

MockitoException (org.mockito.exceptions.base.MockitoException)51 Test (org.junit.Test)29 UnsupportedTerminal (jline.UnsupportedTerminal)12 ConsoleReader (jline.console.ConsoleReader)12 GeogigCLI (org.locationtech.geogig.cli.GeogigCLI)12 Field (java.lang.reflect.Field)4 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)3 ThrowsException (org.mockito.internal.stubbing.answers.ThrowsException)3 ParameterizedConstructorInstantiator (org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 InOrder (org.mockito.InOrder)2 Spy (org.mockito.Spy)2 Instantiator (org.mockito.internal.creation.instance.Instantiator)2 MockHandlerImpl (org.mockito.internal.handler.MockHandlerImpl)2 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)2 FieldInitializationReport (org.mockito.internal.util.reflection.FieldInitializationReport)2