Search in sources :

Example 6 with FieldInfo

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()));
    }
}
Also used : FieldInfo(org.spockframework.runtime.model.FieldInfo)

Example 7 with FieldInfo

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()));
    }
}
Also used : FieldInfo(org.spockframework.runtime.model.FieldInfo)

Example 8 with FieldInfo

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);
}
Also used : ArrayList(java.util.ArrayList) FieldInfo(org.spockframework.runtime.model.FieldInfo)

Example 9 with FieldInfo

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);
        }
    }
}
Also used : Field(java.lang.reflect.Field) Autobuild(org.apache.tapestry5.ioc.annotations.Autobuild) InjectService(org.apache.tapestry5.ioc.annotations.InjectService) FieldInfo(org.spockframework.runtime.model.FieldInfo)

Aggregations

FieldInfo (org.spockframework.runtime.model.FieldInfo)9 TestRule (org.junit.rules.TestRule)2 Statement (org.junit.runners.model.Statement)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Autobuild (org.apache.tapestry5.ioc.annotations.Autobuild)1 InjectService (org.apache.tapestry5.ioc.annotations.InjectService)1 MethodRule (org.junit.rules.MethodRule)1 AutoCleanup (spock.lang.AutoCleanup)1