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");
}
}
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");
}
}
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);
}
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);
}
}
}
}
}
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());
}
}
Aggregations