Search in sources :

Example 1 with MockitoException

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

the class InlineByteBuddyMockMakerTest method should_throw_exception_redefining_array.

@Test
@SuppressWarnings("unchecked")
public void should_throw_exception_redefining_array() {
    int[] array = new int[5];
    MockCreationSettings<? extends int[]> settings = settingsFor(array.getClass());
    try {
        mockMaker.createMock(settings, new MockHandlerImpl(settings));
        fail("Expected a MockitoException");
    } catch (MockitoException e) {
        assertThat(e).hasMessageContaining("Arrays cannot be mocked");
    }
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) MockHandlerImpl(org.mockito.internal.handler.MockHandlerImpl) Test(org.junit.Test)

Example 2 with MockitoException

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

the class InlineByteBuddyMockMakerTest method should_throw_exception_redefining_unmodifiable_class.

@Test
@SuppressWarnings("unchecked")
public void should_throw_exception_redefining_unmodifiable_class() {
    MockCreationSettings settings = settingsFor(int.class);
    try {
        mockMaker.createMock(settings, new MockHandlerImpl(settings));
        fail("Expected a MockitoException");
    } catch (MockitoException e) {
        e.printStackTrace();
        assertThat(e).hasMessageContaining("Could not modify all classes");
    }
}
Also used : MockCreationSettings(org.mockito.mock.MockCreationSettings) MockitoException(org.mockito.exceptions.base.MockitoException) MockHandlerImpl(org.mockito.internal.handler.MockHandlerImpl) Test(org.junit.Test)

Example 3 with MockitoException

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

the class CaptorAnnotationProcessor method process.

public Object process(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 4 with MockitoException

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

the class IndependentAnnotationEngine method process.

@Override
public void process(Class<?> clazz, Object testInstance) {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        boolean alreadyAssigned = false;
        for (Annotation annotation : field.getAnnotations()) {
            Object mock = createMockFor(annotation, field);
            if (mock != null) {
                throwIfAlreadyAssigned(field, alreadyAssigned);
                alreadyAssigned = true;
                try {
                    setField(testInstance, field, mock);
                } catch (Exception e) {
                    throw new MockitoException("Problems setting field " + field.getName() + " annotated with " + annotation, e);
                }
            }
        }
    }
}
Also used : FieldSetter.setField(org.mockito.internal.util.reflection.FieldSetter.setField) Field(java.lang.reflect.Field) MockitoException(org.mockito.exceptions.base.MockitoException) Annotation(java.lang.annotation.Annotation) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 5 with MockitoException

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

the class SpyAnnotationEngine method spyNewInstance.

private static Object spyNewInstance(Object testInstance, Field field) throws InstantiationException, IllegalAccessException, InvocationTargetException {
    MockSettings settings = withSettings().defaultAnswer(CALLS_REAL_METHODS).name(field.getName());
    Class<?> type = field.getType();
    if (type.isInterface()) {
        return Mockito.mock(type, settings.useConstructor());
    }
    int modifiers = type.getModifiers();
    if (typeIsPrivateAbstractInnerClass(type, modifiers)) {
        throw new MockitoException(join("@Spy annotation can't initialize private abstract inner classes.", "  inner class: '" + type.getSimpleName() + "'", "  outer class: '" + type.getEnclosingClass().getSimpleName() + "'", "", "You should augment the visibility of this inner class"));
    }
    if (typeIsNonStaticInnerClass(type, modifiers)) {
        Class<?> enclosing = type.getEnclosingClass();
        if (!enclosing.isInstance(testInstance)) {
            throw new MockitoException(join("@Spy annotation can only initialize inner classes declared in the test.", "  inner class: '" + type.getSimpleName() + "'", "  outer class: '" + enclosing.getSimpleName() + "'", ""));
        }
        return Mockito.mock(type, settings.useConstructor().outerInstance(testInstance));
    }
    Constructor<?> constructor = noArgConstructorOf(type);
    if (Modifier.isPrivate(constructor.getModifiers())) {
        constructor.setAccessible(true);
        return Mockito.mock(type, settings.spiedInstance(constructor.newInstance()));
    } else {
        return Mockito.mock(type, settings.useConstructor());
    }
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) MockSettings(org.mockito.MockSettings)

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