Search in sources :

Example 16 with MockitoException

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

the class VerificationDataImplTest method shouldToStringBeNotVerifiable.

@Test
public void shouldToStringBeNotVerifiable() throws Exception {
    InvocationMatcher toString = new InvocationBuilder().method("toString").toInvocationMatcher();
    try {
        new VerificationDataImpl(null, toString);
        fail();
    } catch (MockitoException e) {
    }
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 17 with MockitoException

use of org.mockito.exceptions.base.MockitoException in project gwt-test-utils by gwt-test-utils.

the class GwtSpyAnnotationEngine method process.

@Override
@SuppressWarnings("deprecation")
public // for MockitoAnnotations.Mock
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, com.googlecode.gwt.test.Mock.class, Mock.class, org.mockito.MockitoAnnotations.Mock.class, Captor.class);
            Object instance = null;
            try {
                FieldInitializationReport report = new FieldInitializer(testInstance, field).initialize();
                instance = report.fieldInstance();
            } catch (MockitoException e) {
                new Reporter().cannotInitializeForSpyAnnotation(field.getName(), e);
            }
            try {
                if (new MockUtil().isMock(instance)) {
                    // instance has been spied earlier
                    // for example happens when MockitoAnnotations.initMocks is called two times.
                    Mockito.reset(instance);
                } else {
                    field.setAccessible(true);
                    field.set(testInstance, Mockito.mock(instance.getClass(), withSettings().spiedInstance(instance).defaultAnswer(Mockito.CALLS_REAL_METHODS).name(field.getName())));
                }
            } catch (IllegalAccessException e) {
                throw new MockitoException("Problems initiating spied field " + field.getName(), e);
            }
        }
    }
}
Also used : FieldInitializationReport(org.mockito.internal.util.reflection.FieldInitializationReport) MockUtil(org.mockito.internal.util.MockUtil) Reporter(org.mockito.exceptions.Reporter) FieldInitializer(org.mockito.internal.util.reflection.FieldInitializer) Field(java.lang.reflect.Field) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 18 with MockitoException

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

the class ClassImposterizer method createProxyClass.

public Class<Factory> createProxyClass(Class<?> mockedType, Class<?>... interfaces) {
    if (mockedType == Object.class) {
        mockedType = ClassWithSuperclassToWorkAroundCglibBug.class;
    }
    Enhancer enhancer = new Enhancer() {

        @Override
        @SuppressWarnings("unchecked")
        protected void filterConstructors(Class sc, List constructors) {
        // Don't filter
        }
    };
    Class<?>[] allMockedTypes = prepend(mockedType, interfaces);
    enhancer.setClassLoader(SearchingClassLoader.combineLoadersOf(allMockedTypes));
    enhancer.setUseFactory(true);
    if (mockedType.isInterface()) {
        enhancer.setSuperclass(Object.class);
        enhancer.setInterfaces(allMockedTypes);
    } else {
        enhancer.setSuperclass(mockedType);
        enhancer.setInterfaces(interfaces);
    }
    enhancer.setCallbackTypes(new Class[] { MethodInterceptor.class, NoOp.class });
    enhancer.setCallbackFilter(IGNORE_BRIDGE_METHODS);
    if (mockedType.getSigners() != null) {
        enhancer.setNamingPolicy(NAMING_POLICY_THAT_ALLOWS_IMPOSTERISATION_OF_CLASSES_IN_SIGNED_PACKAGES);
    } else {
        enhancer.setNamingPolicy(MockitoNamingPolicy.INSTANCE);
    }
    enhancer.setSerialVersionUID(42L);
    try {
        return enhancer.createClass();
    } catch (CodeGenerationException e) {
        if (Modifier.isPrivate(mockedType.getModifiers())) {
            throw new MockitoException("\n" + "Mockito cannot mock this class: " + mockedType + ".\n" + "Most likely it is a private class that is not visible by Mockito");
        }
        throw new MockitoException("\n" + "Mockito cannot mock this class: " + mockedType + "\n" + "Mockito can only mock visible & non-final classes." + "\n" + "If you're not sure why you're getting this error, please report to the mailing list.", e);
    }
}
Also used : Enhancer(org.powermock.api.mockito.repackaged.cglib.proxy.Enhancer) CodeGenerationException(org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException) MockitoException(org.mockito.exceptions.base.MockitoException) List(java.util.List)

Example 19 with MockitoException

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

the class AnnotationEnabler method processAnnotationOn.

private Object processAnnotationOn(Captor annotation, Field field) {
    Class<?> type = field.getType();
    if (!ArgumentCaptor.class.isAssignableFrom(type)) {
        throw new MockitoException("@Captor field must be of the type ArgumentCaptor.\n" + "Field: '" + field.getName() + "' has wrong type\n" + "For info how to use @Captor annotations see examples in javadoc for MockitoAnnotations class.");
    }
    Class cls = new GenericMaster().getGenericType(field);
    return ArgumentCaptor.forClass(cls);
}
Also used : GenericMaster(org.mockito.internal.util.reflection.GenericMaster) ArgumentCaptor(org.mockito.ArgumentCaptor) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 20 with MockitoException

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

the class PowerMockitoSpyAnnotationEngine method process.

@SuppressWarnings("deprecation")
@Override
public void process(Class<?> context, Object testClass) {
    Field[] fields = context.getDeclaredFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(Spy.class)) {
            try {
                Whitebox.invokeMethod(this, Spy.class, field, new Class<?>[] { Mock.class, org.mockito.Mock.class, Captor.class });
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e1) {
                throw new RuntimeException(e1);
            }
            boolean wasAccessible = field.isAccessible();
            field.setAccessible(true);
            try {
                Object instance = field.get(testClass);
                if (instance == null) {
                    throw new MockitoException("Cannot create a @Spy for '" + field.getName() + "' field because the *instance* is missing\n" + "Example of correct usage of @Spy:\n" + "   @Spy List mock = new LinkedList();\n");
                }
                field.set(testClass, PowerMockito.spy(instance));
            } catch (IllegalAccessException e) {
                throw new MockitoException("Problems initiating spied field " + field.getName(), e);
            } finally {
                field.setAccessible(wasAccessible);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) MockitoException(org.mockito.exceptions.base.MockitoException) MockitoException(org.mockito.exceptions.base.MockitoException)

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