use of org.spockframework.runtime.model.FieldInfo in project spock by spockframework.
the class SpockDefinition method validate.
private void validate() {
FieldInfo fieldInfo = getFieldInfo();
Assert.that(fieldInfo.isAnnotationPresent(SpringBean.class), "SpringBean annotation is required for this field: '%s.%s:%d' ", fieldInfo.getParent().getName(), fieldInfo.getName(), fieldInfo.getLine());
if (!fieldInfo.hasInitializer()) {
throw new SpringExtensionException(String.format("Field '%s.%s:%d' needs to have an initializer, e.g. List l = Mock()", fieldInfo.getParent().getName(), fieldInfo.getName(), fieldInfo.getLine()));
}
if (Object.class.equals(fieldInfo.getType())) {
throw new SpringExtensionException(String.format("Field '%s.%s:%d' must use a concrete type, not def or Object.", fieldInfo.getParent().getName(), fieldInfo.getName(), fieldInfo.getLine()));
}
}
use of org.spockframework.runtime.model.FieldInfo in project spock by spockframework.
the class SpyDefinition method validate.
private void validate() {
FieldInfo fieldInfo = getFieldInfo();
Assert.that(fieldInfo.isAnnotationPresent(SpringSpy.class), "SpringBean annotation is required for this field: '%s.%s:%d' ", fieldInfo.getParent().getName(), fieldInfo.getName(), fieldInfo.getLine());
if (fieldInfo.hasInitializer()) {
throw new SpringExtensionException(String.format("Field '%s.%s:%d' may not have an initializer.", fieldInfo.getParent().getName(), fieldInfo.getName(), fieldInfo.getLine()));
}
if (Object.class.equals(fieldInfo.getType())) {
throw new SpringExtensionException(String.format("Field '%s.%s:%d' must use a concrete type, not def or Object.", fieldInfo.getParent().getName(), fieldInfo.getName(), fieldInfo.getLine()));
}
}
use of org.spockframework.runtime.model.FieldInfo in project spock by spockframework.
the class RuleExtension method visitSpec.
public void visitSpec(SpecInfo spec) {
if (ruleClass == null)
return;
List<FieldInfo> methodRuleFields = new ArrayList<FieldInfo>();
List<FieldInfo> testRuleFields = new ArrayList<FieldInfo>();
for (FieldInfo field : spec.getAllFields()) {
if (!field.isAnnotationPresent(ruleClass))
continue;
checkIsInstanceField(field);
if (hasFieldType(field, methodRuleClass)) {
methodRuleFields.add(field);
} else if (hasFieldType(field, testRuleClass)) {
testRuleFields.add(field);
} else {
invalidFieldType(field);
}
}
if (!methodRuleFields.isEmpty())
MethodRuleInterceptorInstaller.install(spec, methodRuleFields);
if (!testRuleFields.isEmpty())
TestRuleInterceptorInstaller.install(spec, testRuleFields);
}
use of org.spockframework.runtime.model.FieldInfo in project spock by spockframework.
the class TapestryInterceptor method injectServices.
private void injectServices(Object target, boolean sharedFields) throws IllegalAccessException {
for (final FieldInfo field : spec.getAllFields()) {
Field rawField = field.getReflection();
if ((rawField.isAnnotationPresent(Inject.class) || ReflectionUtil.isAnnotationPresent(rawField, "javax.inject.Inject") || rawField.isAnnotationPresent(Autobuild.class)) && rawField.isAnnotationPresent(Shared.class) == sharedFields) {
Object value = registry.getObject(rawField.getType(), createAnnotationProvider(field));
rawField.setAccessible(true);
rawField.set(target, value);
} else if (rawField.isAnnotationPresent(InjectService.class)) {
String serviceName = rawField.getAnnotation(InjectService.class).value();
Object value = registry.getService(serviceName, rawField.getType());
rawField.setAccessible(true);
rawField.set(target, value);
}
}
}
Aggregations