Search in sources :

Example 1 with FeatureInfo

use of org.spockframework.runtime.model.FeatureInfo in project ratpack by ratpack.

the class TempDirExtension method visitFieldAnnotation.

@Override
public void visitFieldAnnotation(TempDir annotation, FieldInfo field) {
    Class<?> fieldType = field.getType();
    if (!fieldType.isAssignableFrom(TemporaryFolder.class)) {
        throw new InvalidSpecException("@TempDir can only be used on TemporaryFolder");
    }
    TempDirInterceptor interceptor = new TempDirInterceptor(fieldType, field, configuration.baseDir, configuration.keep);
    // attach interceptor
    SpecInfo specInfo = field.getParent();
    if (field.isShared()) {
        specInfo.addInterceptor(interceptor);
    } else {
        for (FeatureInfo featureInfo : specInfo.getBottomSpec().getAllFeatures()) {
            featureInfo.addIterationInterceptor(interceptor);
        }
    }
}
Also used : SpecInfo(org.spockframework.runtime.model.SpecInfo) InvalidSpecException(org.spockframework.runtime.InvalidSpecException) FeatureInfo(org.spockframework.runtime.model.FeatureInfo)

Example 2 with FeatureInfo

use of org.spockframework.runtime.model.FeatureInfo in project gradle by gradle.

the class MultiTestExtension method visitSpecAnnotation.

@Override
public void visitSpecAnnotation(T annotation, SpecInfo spec) {
    if (!spec.getFeatures().isEmpty()) {
        AbstractMultiTestInterceptor interceptor = makeInterceptor(spec.getBottomSpec().getReflection());
        spec.addInitializerInterceptor(interceptor);
        for (FeatureInfo feature : spec.getFeatures()) {
            interceptor.interceptFeature(feature);
        }
    }
}
Also used : FeatureInfo(org.spockframework.runtime.model.FeatureInfo)

Example 3 with FeatureInfo

use of org.spockframework.runtime.model.FeatureInfo in project micronaut-test by micronaut-projects.

the class MicronautSpockExtension method visitSpecAnnotation.

@Override
public void visitSpecAnnotation(T annotation, SpecInfo spec) {
    spec.getAllFeatures().forEach(feature -> {
        feature.addInterceptor(invocation -> {
            try {
                beforeTestMethod(buildContext(invocation, null));
                invocation.proceed();
            } finally {
                afterTestMethod(buildContext(invocation, null));
            }
        });
        feature.getFeatureMethod().addInterceptor(invocation -> {
            try {
                beforeTestExecution(buildContext(invocation, null));
                invocation.proceed();
                afterTestExecution(buildContext(invocation, null));
            } catch (Throwable e) {
                afterTestExecution(buildContext(invocation, e));
                throw e;
            }
        });
    });
    spec.addSetupSpecInterceptor(invocation -> {
        MicronautTest micronautTest = spec.getAnnotation(MicronautTest.class);
        MicronautTestValue micronautTestValue = buildValueObject(micronautTest);
        beforeClass(invocation, spec.getReflection(), micronautTestValue);
        if (specDefinition == null) {
            if (!isTestSuiteBeanPresent(spec.getReflection())) {
                throw new InvalidSpecException(MISCONFIGURED_MESSAGE);
            } else {
                final List<FeatureInfo> features = invocation.getSpec().getFeatures();
                for (FeatureInfo feature : features) {
                    feature.setSkipped(true);
                }
            }
        } else {
            List<FieldInfo> fields = spec.getAllFields();
            for (FieldInfo field : fields) {
                if (field.isShared() && field.getAnnotation(Inject.class) != null) {
                    applicationContext.inject(invocation.getSharedInstance());
                    break;
                }
            }
        }
        beforeTestClass(buildContext(invocation, null));
        invocation.proceed();
    });
    spec.addCleanupSpecInterceptor(invocation -> {
        afterTestClass(buildContext(invocation, null));
        afterClass(invocation);
        invocation.proceed();
        singletonMocks.clear();
    });
    spec.addSetupInterceptor(invocation -> {
        final Object instance = invocation.getInstance();
        final Method method = invocation.getFeature().getFeatureMethod().getReflection();
        List<Property> propertyAnnotations = Arrays.asList(method.getAnnotationsByType(Property.class));
        beforeEach(invocation, instance, method, propertyAnnotations);
        for (Object mock : creatableMocks) {
            mockUtil.attachMock(mock, (Specification) instance);
        }
        for (Object mock : singletonMocks) {
            mockUtil.attachMock(mock, (Specification) instance);
        }
        try {
            beforeSetupTest(buildContext(invocation, null));
            invocation.proceed();
        } finally {
            afterSetupTest(buildContext(invocation, null));
        }
    });
    spec.addCleanupInterceptor(invocation -> {
        for (Object mock : creatableMocks) {
            mockUtil.detachMock(mock);
        }
        for (Object mock : singletonMocks) {
            mockUtil.detachMock(mock);
        }
        creatableMocks.clear();
        afterEach(invocation);
        try {
            beforeCleanupTest(buildContext(invocation, null));
            invocation.proceed();
        } finally {
            afterCleanupTest(buildContext(invocation, null));
        }
    });
}
Also used : InvalidSpecException(org.spockframework.runtime.InvalidSpecException) FeatureInfo(org.spockframework.runtime.model.FeatureInfo) MicronautTestValue(io.micronaut.test.annotation.MicronautTestValue) Method(java.lang.reflect.Method) Property(io.micronaut.context.annotation.Property) MicronautTest(io.micronaut.test.extensions.spock.annotation.MicronautTest) FieldInfo(org.spockframework.runtime.model.FieldInfo)

Aggregations

FeatureInfo (org.spockframework.runtime.model.FeatureInfo)3 InvalidSpecException (org.spockframework.runtime.InvalidSpecException)2 Property (io.micronaut.context.annotation.Property)1 MicronautTestValue (io.micronaut.test.annotation.MicronautTestValue)1 MicronautTest (io.micronaut.test.extensions.spock.annotation.MicronautTest)1 Method (java.lang.reflect.Method)1 FieldInfo (org.spockframework.runtime.model.FieldInfo)1 SpecInfo (org.spockframework.runtime.model.SpecInfo)1