use of org.mockito.exceptions.base.MockitoException in project j2objc by google.
the class IosMockMaker method createMock.
@Override
@SuppressWarnings("unchecked")
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
Class<T> typeToMock = settings.getTypeToMock();
@SuppressWarnings("rawtypes") Set<Class> interfacesSet = settings.getExtraInterfaces();
Class<?>[] extraInterfaces = interfacesSet.toArray(new Class[interfacesSet.size()]);
InvocationHandler invocationHandler = new InvocationHandlerAdapter(handler);
if (typeToMock.isInterface()) {
// support interfaces via java.lang.reflect.Proxy
@SuppressWarnings("rawtypes") Class[] classesToMock = new Class[extraInterfaces.length + 1];
classesToMock[0] = typeToMock;
System.arraycopy(extraInterfaces, 0, classesToMock, 1, extraInterfaces.length);
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
// newProxyInstance returns the type of typeToMock
T mock = (T) Proxy.newProxyInstance(contextClassLoader, classesToMock, invocationHandler);
return mock;
} else {
try {
Class<? extends T> proxyClass = getProxyClass(typeToMock, extraInterfaces);
T mock = proxyClass.newInstance();
((ClassProxy) mock).setHandler(invocationHandler);
return mock;
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new MockitoException("Failed to mock " + typeToMock, e);
}
}
}
use of org.mockito.exceptions.base.MockitoException in project mockito by mockito.
the class InlineByteBuddyMockMaker method createMock.
@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
Class<? extends T> type = createMockType(settings);
Instantiator instantiator = Plugins.getInstantiatorProvider().getInstantiator(settings);
try {
T instance = instantiator.newInstance(type);
MockMethodInterceptor mockMethodInterceptor = new MockMethodInterceptor(asInternalMockHandler(handler), settings);
mocks.put(instance, mockMethodInterceptor);
if (instance instanceof MockAccess) {
((MockAccess) instance).setMockitoInterceptor(mockMethodInterceptor);
}
return instance;
} catch (org.mockito.internal.creation.instance.InstantiationException e) {
throw new MockitoException("Unable to create mock instance of type '" + type.getSimpleName() + "'", e);
}
}
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);
}
}
}
use of org.mockito.exceptions.base.MockitoException in project mockito by mockito.
the class SpyAnnotationEngine method process.
@Override
public 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, Mock.class, Captor.class);
field.setAccessible(true);
Object instance;
try {
instance = field.get(testInstance);
if (MockUtil.isMock(instance)) {
// instance has been spied earlier
// for example happens when MockitoAnnotations.initMocks is called two times.
Mockito.reset(instance);
} else if (instance != null) {
field.set(testInstance, spyInstance(field, instance));
} else {
field.set(testInstance, spyNewInstance(testInstance, field));
}
} catch (Exception e) {
throw new MockitoException("Unable to initialize @Spy annotated field '" + field.getName() + "'.\n" + e.getMessage(), e);
}
}
}
}
use of org.mockito.exceptions.base.MockitoException in project mockito by mockito.
the class ConstructorInjection method processInjection.
public boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
try {
SimpleArgumentResolver simpleArgumentResolver = new SimpleArgumentResolver(mockCandidates);
FieldInitializationReport report = new FieldInitializer(fieldOwner, field, simpleArgumentResolver).initialize();
return report.fieldWasInitializedUsingContructorArgs();
} catch (MockitoException e) {
if (e.getCause() instanceof InvocationTargetException) {
Throwable realCause = e.getCause().getCause();
throw fieldInitialisationThrewException(field, realCause);
}
// other causes should be fine
return false;
}
}
Aggregations