24kpure commented on issue #9116:
URL: https://github.com/apache/dubbo/issues/9116#issuecomment-952469354


   Why not mock dubboReference?  Here is a best practice for reference 
@stonelion.
   ```
   class ReferenceBeanProcessor implements BeanPostProcessor, 
BeanFactoryPostProcessor {
   
       private ConfigurableListableBeanFactory beanFactory;
   
       @Override
       public Object postProcessBeforeInitialization(Object bean, String 
beanName) throws BeansException {
           return bean;
       }
   
       @Override
       public void postProcessBeanFactory(ConfigurableListableBeanFactory 
beanFactory) throws BeansException {
           this.beanFactory = beanFactory;
       }
   
       @Override
       public Object postProcessAfterInitialization(Object bean, String 
beanName) throws BeansException {
           List<Field> annotatedFields = 
FieldUtils.getFieldsListWithAnnotation(bean.getClass(), Reference.class);
           
annotatedFields.addAll(FieldUtils.getFieldsListWithAnnotation(bean.getClass(), 
com.alibaba.dubbo.config.annotation.Reference.class));
           
annotatedFields.addAll(FieldUtils.getFieldsListWithAnnotation(bean.getClass(), 
DubboReference.class));
           for (Field field : annotatedFields) {
               try {
                   Class<?> type = field.getType();
                   String mockBeanName = 
BeanFactoryUtils.transformedBeanName(type.getSimpleName());
                   if (!beanFactory.containsBean(mockBeanName)) {
                       beanFactory.registerSingleton(mockBeanName, 
Mockito.mock(type));
                   }
   
                   boolean accessible = field.isAccessible();
                   field.setAccessible(true);
                   field.set(bean, beanFactory.getBean(type));
                   field.setAccessible(accessible);
               } catch (IllegalAccessException e) {
               }
           }
           return bean;
       }
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to