Search in sources :

Example 6 with MockitoException

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

the class SpyOnInjectedFieldsHandler method processInjection.

@Override
protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
    FieldReader fieldReader = new FieldReader(fieldOwner, field);
    // TODO refoctor : code duplicated in SpyAnnotationEngine
    if (!fieldReader.isNull() && field.isAnnotationPresent(Spy.class)) {
        try {
            Object instance = fieldReader.read();
            if (MockUtil.isMock(instance)) {
                // A. instance has been spied earlier
                // B. protect against multiple use of MockitoAnnotations.initMocks()
                Mockito.reset(instance);
            } else {
                Object mock = Mockito.mock(instance.getClass(), withSettings().spiedInstance(instance).defaultAnswer(Mockito.CALLS_REAL_METHODS).name(field.getName()));
                setField(fieldOwner, field, mock);
            }
        } catch (Exception e) {
            throw new MockitoException("Problems initiating spied field " + field.getName(), e);
        }
    }
    return false;
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) FieldReader(org.mockito.internal.util.reflection.FieldReader) Spy(org.mockito.Spy) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 7 with MockitoException

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

the class PluginFinder method findPluginClass.

String findPluginClass(Iterable<URL> resources) {
    for (URL resource : resources) {
        InputStream s = null;
        try {
            s = resource.openStream();
            String pluginClassName = new PluginFileReader().readPluginClass(s);
            if (pluginClassName == null) {
                //If the resource does not have plugin class name we're ignoring it
                continue;
            }
            if (!pluginSwitch.isEnabled(pluginClassName)) {
                continue;
            }
            return pluginClassName;
        } catch (Exception e) {
            throw new MockitoException("Problems reading plugin implementation from: " + resource, e);
        } finally {
            IOUtil.closeQuietly(s);
        }
    }
    return null;
}
Also used : InputStream(java.io.InputStream) MockitoException(org.mockito.exceptions.base.MockitoException) URL(java.net.URL) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 8 with MockitoException

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

the class SubclassByteBuddyMockMaker method createMock.

@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    Class<? extends T> mockedProxyType = createMockType(settings);
    Instantiator instantiator = Plugins.getInstantiatorProvider().getInstantiator(settings);
    T mockInstance = null;
    try {
        mockInstance = instantiator.newInstance(mockedProxyType);
        MockAccess mockAccess = (MockAccess) mockInstance;
        mockAccess.setMockitoInterceptor(new MockMethodInterceptor(asInternalMockHandler(handler), settings));
        return ensureMockIsAssignableToMockedType(settings, mockInstance);
    } catch (ClassCastException cce) {
        throw new MockitoException(join("ClassCastException occurred while creating the mockito mock :", "  class to mock : " + describeClass(settings.getTypeToMock()), "  created class : " + describeClass(mockedProxyType), "  proxy instance class : " + describeClass(mockInstance), "  instance creation by : " + instantiator.getClass().getSimpleName(), "", "You might experience classloading issues, please ask the mockito mailing-list.", ""), cce);
    } catch (org.mockito.internal.creation.instance.InstantiationException e) {
        throw new MockitoException("Unable to create mock instance of type '" + mockedProxyType.getSuperclass().getSimpleName() + "'", e);
    }
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) Instantiator(org.mockito.internal.creation.instance.Instantiator)

Example 9 with MockitoException

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

the class MockitoAnnotations method initMocks.

/**
     * Initializes objects annotated with Mockito annotations for given testClass:
     *  &#064;{@link org.mockito.Mock}, &#064;{@link Spy}, &#064;{@link Captor}, &#064;{@link InjectMocks}
     * <p>
     * See examples in javadoc for {@link MockitoAnnotations} class.
     */
public static void initMocks(Object testClass) {
    if (testClass == null) {
        throw new MockitoException("testClass cannot be null. For info how to use @Mock annotations see examples in javadoc for MockitoAnnotations class");
    }
    AnnotationEngine annotationEngine = new GlobalConfiguration().tryGetPluginAnnotationEngine();
    annotationEngine.process(testClass.getClass(), testClass);
}
Also used : GlobalConfiguration(org.mockito.internal.configuration.GlobalConfiguration) MockitoException(org.mockito.exceptions.base.MockitoException) AnnotationEngine(org.mockito.plugins.AnnotationEngine)

Example 10 with MockitoException

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

the class InvocationImplTest method shouldScreamWhenCallingRealMethodOnInterface.

@Test
public void shouldScreamWhenCallingRealMethodOnInterface() throws Throwable {
    //given
    Invocation invocationOnInterface = new InvocationBuilder().toInvocation();
    try {
        //when
        invocationOnInterface.callRealMethod();
        //then
        fail();
    } catch (MockitoException e) {
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) MockitoException(org.mockito.exceptions.base.MockitoException) Test(org.junit.Test)

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