Search in sources :

Example 1 with MicronautTestValue

use of io.micronaut.test.annotation.MicronautTestValue in project micronaut-test by micronaut-projects.

the class MicronautJunit5Extension method beforeAll.

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
    final Class<?> testClass = extensionContext.getRequiredTestClass();
    MicronautTestValue micronautTestValue = buildMicronautTestValue(testClass);
    beforeClass(extensionContext, testClass, micronautTestValue);
    getStore(extensionContext).put(ApplicationContext.class, applicationContext);
    if (specDefinition != null) {
        TestInstance ti = AnnotationSupport.findAnnotation(testClass, TestInstance.class).orElse(null);
        if (ti != null && ti.value() == TestInstance.Lifecycle.PER_CLASS) {
            Object testInstance = extensionContext.getRequiredTestInstance();
            applicationContext.inject(testInstance);
        }
    }
    beforeTestClass(buildContext(extensionContext));
}
Also used : TestInstance(org.junit.jupiter.api.TestInstance) MicronautTestValue(io.micronaut.test.annotation.MicronautTestValue)

Example 2 with MicronautTestValue

use of io.micronaut.test.annotation.MicronautTestValue 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

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