Search in sources :

Example 6 with InvalidSpecException

use of org.spockframework.runtime.InvalidSpecException 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

InvalidSpecException (org.spockframework.runtime.InvalidSpecException)6 FeatureInfo (org.spockframework.runtime.model.FeatureInfo)2 Rollup (spock.lang.Rollup)2 Unroll (spock.lang.Unroll)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 ArrayList (java.util.ArrayList)1 DynamicProxyMockInterceptorAdapter (org.spockframework.mock.runtime.DynamicProxyMockInterceptorAdapter)1 FieldInfo (org.spockframework.runtime.model.FieldInfo)1 SpecInfo (org.spockframework.runtime.model.SpecInfo)1