Search in sources :

Example 41 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 42 with MockitoException

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

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

Example 44 with MockitoException

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

the class ShpImportTest method testImportException.

@Test
public void testImportException() throws Exception {
    ConsoleReader consoleReader = new ConsoleReader(System.in, System.out, new UnsupportedTerminal());
    GeogigCLI mockCli = spy(new GeogigCLI(consoleReader));
    setUpGeogig(mockCli);
    when(mockCli.getConsole()).thenThrow(new MockitoException("Exception"));
    ShpImport importCommand = new ShpImport();
    importCommand.shapeFile = new ArrayList<String>();
    importCommand.shapeFile.add(ShpImport.class.getResource("shape.shp").getFile());
    exception.expect(MockitoException.class);
    importCommand.run(mockCli);
}
Also used : GeogigCLI(org.locationtech.geogig.cli.GeogigCLI) ConsoleReader(jline.console.ConsoleReader) UnsupportedTerminal(jline.UnsupportedTerminal) MockitoException(org.mockito.exceptions.base.MockitoException) Test(org.junit.Test)

Example 45 with MockitoException

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

the class SQLServerImportTest method testImportException.

@Test
public void testImportException() throws Exception {
    ConsoleReader consoleReader = new ConsoleReader(System.in, System.out, new UnsupportedTerminal());
    GeogigCLI mockCli = spy(new GeogigCLI(consoleReader));
    setUpGeogig(mockCli);
    when(mockCli.getConsole()).thenThrow(new MockitoException("Exception"));
    SQLServerImport importCommand = new SQLServerImport();
    importCommand.all = true;
    importCommand.dataStoreFactory = TestHelper.createTestFactory();
    exception.expect(MockitoException.class);
    importCommand.run(mockCli);
}
Also used : GeogigCLI(org.locationtech.geogig.cli.GeogigCLI) ConsoleReader(jline.console.ConsoleReader) UnsupportedTerminal(jline.UnsupportedTerminal) 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