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