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
}
}
}
});
}
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);
}
}
}
}
}
}
Aggregations