In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is the Dubbo bean method of Mock injection". In the daily operation, I believe that many people have doubts about what the Dubbo bean method of Mock injection is. The editor consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "what is the Dubbo bean method of Mock injection?" Next, please follow the editor to study!
Recently, I refactored the back-end service with Spring cloud Alibaba, and during the unit test with Mockit, I found that the remote interface of dubbo would still be called. Debug finds that the @ Reference annotated interface and the @ MockBean object get two instances from the ApplicationContext, so do another layer on your own, and change it to a Mock proxy instance after Spring starts.
The code is as follows
Import org.apache.dubbo.config.annotation.Reference;import org.springframework.beans.BeansException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.context.ApplicationContext;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import java.lang.reflect.Field;import java.util.List;import java.util.Map;@Order@Componentpublic class MockerFactory implements BeanPostProcessor {@ Autowired private ApplicationContext applicationContext @ Override public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException {return bean;} @ Override public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException {/ / only deals with if (bean.getClass (). GetPackage () = null | |! bean.getClass (). GetPackage (). GetName (). Contains ("business package name")) {return bean } List fields = org.apache.commons.lang3.reflect.FieldUtils.getAllFieldsList (bean.getClass ()); for (Field field: fields) {Reference reference = field.getAnnotation (Reference.class); Map beans = applicationContext.getBeansOfType (field.getType ()) / / replace the dubbo interface with the instance of mock proxy if (reference! = null & & beans.size () > 1) {field.setAccessible (true); setMockFieldToBean (bean, field, beans);}} return bean } private void setMockFieldToBean (Object bean, Field field, Map beans) {for (Object fieldBean: beans.values ()) {if (fieldBean.getClass (). GetName (). Contains ("$MockitoMock$")) {try {field.set (bean, fieldBean);} catch (IllegalAccessException e) {throw new RuntimeException (e) At this point, the study on "what is the Dubbo bean method of Mock injection" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.