Search in sources :

Example 1 with InterceptedProxy

use of io.micronaut.aop.InterceptedProxy in project micronaut-test by micronaut-projects.

the class MicronautJunit5Extension method alignMocks.

@Override
protected void alignMocks(ExtensionContext context, Object instance) {
    if (specDefinition == null) {
        return;
    }
    findSpecInstance(context).ifPresent(specInstance -> {
        for (FieldInjectionPoint injectedField : specDefinition.getInjectedFields()) {
            final boolean isMock = applicationContext.resolveMetadata(injectedField.getType()).isAnnotationPresent(MockBean.class);
            if (isMock) {
                final Field field = injectedField.getField();
                field.setAccessible(true);
                try {
                    final Object mock = field.get(specInstance);
                    if (mock instanceof InterceptedProxy) {
                        InterceptedProxy ip = (InterceptedProxy) mock;
                        final Object target = ip.interceptedTarget();
                        field.set(specInstance, target);
                    }
                } catch (IllegalAccessException e) {
                // continue
                }
            }
        }
    });
}
Also used : Field(java.lang.reflect.Field) InterceptedProxy(io.micronaut.aop.InterceptedProxy) FieldInjectionPoint(io.micronaut.inject.FieldInjectionPoint)

Example 2 with InterceptedProxy

use of io.micronaut.aop.InterceptedProxy in project micronaut-test by micronaut-projects.

the class MicronautSpockExtension method alignMocks.

@Override
protected void alignMocks(IMethodInvocation context, Object instance) {
    for (MethodInjectionPoint injectedMethod : specDefinition.getInjectedMethods()) {
        final Argument<?>[] args = injectedMethod.getArguments();
        if (args.length == 1) {
            final Optional<FieldInfo> fld = context.getSpec().getAllFields().stream().filter(f -> f.getName().equals(args[0].getName())).findFirst();
            if (fld.isPresent()) {
                final FieldInfo fieldInfo = fld.get();
                final Object fieldInstance = fieldInfo.readValue(instance);
                if (fieldInstance instanceof InterceptedProxy) {
                    Object interceptedTarget = ((InterceptedProxy) fieldInstance).interceptedTarget();
                    if (mockUtil.isMock(interceptedTarget)) {
                        fieldInfo.writeValue(instance, interceptedTarget);
                    }
                }
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) InterceptedProxy(io.micronaut.aop.InterceptedProxy) TestContext(io.micronaut.test.context.TestContext) SpecInfo(org.spockframework.runtime.model.SpecInfo) IMethodInvocation(org.spockframework.runtime.extension.IMethodInvocation) Map(java.util.Map) FieldInfo(org.spockframework.runtime.model.FieldInfo) BeanCreatedEventListener(io.micronaut.context.event.BeanCreatedEventListener) Argument(io.micronaut.core.type.Argument) FeatureInfo(org.spockframework.runtime.model.FeatureInfo) Method(java.lang.reflect.Method) Property(io.micronaut.context.annotation.Property) IAnnotationDrivenExtension(org.spockframework.runtime.extension.IAnnotationDrivenExtension) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) MockUtil(org.spockframework.mock.MockUtil) List(java.util.List) MethodInjectionPoint(io.micronaut.inject.MethodInjectionPoint) MicronautTestValue(io.micronaut.test.annotation.MicronautTestValue) MethodInfo(org.spockframework.runtime.model.MethodInfo) CollectionUtils(io.micronaut.core.util.CollectionUtils) MicronautTest(io.micronaut.test.extensions.spock.annotation.MicronautTest) Annotation(java.lang.annotation.Annotation) AbstractMicronautExtension(io.micronaut.test.extensions.AbstractMicronautExtension) Optional(java.util.Optional) InvalidSpecException(org.spockframework.runtime.InvalidSpecException) Specification(spock.lang.Specification) Queue(java.util.Queue) Inject(jakarta.inject.Inject) TestPropertyProvider(io.micronaut.test.support.TestPropertyProvider) Argument(io.micronaut.core.type.Argument) InterceptedProxy(io.micronaut.aop.InterceptedProxy) MethodInjectionPoint(io.micronaut.inject.MethodInjectionPoint) FieldInfo(org.spockframework.runtime.model.FieldInfo)

Aggregations

InterceptedProxy (io.micronaut.aop.InterceptedProxy)2 Property (io.micronaut.context.annotation.Property)1 BeanCreatedEventListener (io.micronaut.context.event.BeanCreatedEventListener)1 Argument (io.micronaut.core.type.Argument)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 FieldInjectionPoint (io.micronaut.inject.FieldInjectionPoint)1 MethodInjectionPoint (io.micronaut.inject.MethodInjectionPoint)1 MicronautTestValue (io.micronaut.test.annotation.MicronautTestValue)1 TestContext (io.micronaut.test.context.TestContext)1 AbstractMicronautExtension (io.micronaut.test.extensions.AbstractMicronautExtension)1 MicronautTest (io.micronaut.test.extensions.spock.annotation.MicronautTest)1 TestPropertyProvider (io.micronaut.test.support.TestPropertyProvider)1 Inject (jakarta.inject.Inject)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1