jmockit Chinese network dubbo bean

Dubbo is our common RPC framework, when writing unit tests need to call Dubbo consumer Bean, Bean how to simulate the behavior of Dubbo consumption of it?

   Take e-mail, it is usually in the code, we aim to call Dubbo mail service to send e-mail to complete, so we will send a good message in the Spring configuration of Dubbo consumer Bean,
Dubbo-consumer.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<? xml  version = "1.0"  encoding = "utf-8" ?>
< beans  xmlns = "http://www.springframework.org/schema/beans"
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     xmlns:dubbo = "http://dubbo.apache.org/schema/dubbo"
     xsi:schemaLocation="http://www.springframework.org/schema/beans        
     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        
     http://dubbo.apache.org/schema/dubbo        
     http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
     < dubbo:application  name = "consumer-of-dubbo-mock-test"  />
 
     < dubbo:registry  address = "zookeeper://your-zk-address:2181"  />
     <!-- 生成远程服务代理,可以和本地bean一样使用mailService -->
     < dubbo:reference  id = "mailService"  interface = "cn.jmockit.demos.MailService"  />
</ beans >

 

 

   Dubbo familiar friends all know, the above xml configuration is the basic configuration of Dubbo, equipped zookeeper address dubbo services, also equipped with the name of Dubbo mailService consumer Bean, for sending e-mail in your application.
 
   When we run the unit tests, if zookeeper Rom or mailService service provider does not exist, it will result in Spring initialization failure, and we do not really want to send the message (unless it is in order to test sending mail). So we hope to be mailService Mock.

  The following presents a Mock Dubbo consumer Bean's program:

  1. In the spring before the initialization, carry out all Dubbo Mock Bean consumption, ie <dubbo> tag of the local interface return the default implementation.

  2. If you want to be certain Dubbo consumer Bean Mock, the custom of Dubbo Bean consumption can be achieved.



   Look at the test code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//dubbo消费bean Mock 
@SuppressWarnings ({  "unchecked" "rawtypes"  })
@ContextConfiguration (locations = {  "/META-INF/dubbo-consumer.xml"  })
@RunWith (SpringJUnit4ClassRunner. class )
public  class  DubboConsumerBeanMockingTest {
     // 这里要@BeforeClass,因为要抢在spring加载dubbo前,对dubbo的消费工厂bean
     // ReferenceBean进行mock,不然dubbo可能因为连上不zk或无法找不
     // 服务的提供者等原因而无法初始化的,进而,单元测试运行不下去
     @BeforeClass
     public  static  void  mockDubbo() {
         // 你准备mock哪个消费bean
         // 比如要对dubbo-consumber.xml里配置的cn.jmockit.demos.MailService这个消费bean进行mock
         Map<String, Object> mockMap =  new  HashMap<String, Object>();
         mockMap.put( "cn.jmockit.demos.MailService" new  MockUp(MailService. class ) {
             // 在这里书写对这个消费bean进行mock的mock逻辑,想mock哪个方法,就自行添加,注意方法一定要加上@Mock注解哦
             @Mock
             public  boolean  sendMail( long  userId, String content) {
                 // 单元测试时,不需要调用邮件服务器发送邮件,这里统一mock邮件发送成功
                 return  true ;
             }
         }.getMockInstance());
         // 如果要Mock其它的消费bean,自行添加,mockMap.put.....如上
         new  DubboConsumerBeanMockUp(mockMap);
     }
 
     // 现在你使用的dubbo消费bean就是本地mock过的了,并不是指向远程dubbo服务的bean了
     @Resource
     MailService mailService;
 
     @Test
     public  void  testSendMail() {
         long  userId =  123456 ;
         String content =  "test mail content" ;
         Assert.isTrue(mailService.sendMail(userId, content));
     }
}

 

 

   The above code, the most critical is DubboConsumerBeanMockUp class, this class Mock all Dubbo consumer Bean.

   Source code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//dubbo消费bean的MockUp(伪类)
@SuppressWarnings ( "rawtypes" )
public  class  DubboConsumerBeanMockUp  extends  MockUp<ReferenceBean> {
     // 自定义的消费bean mock对象
     private  Map<String, Object> mockMap;
 
     public  DubboConsumerBeanMockUp() {
     }
 
     public  DubboConsumerBeanMockUp(Map<String, Object> mockMap) {
         this .mockMap = mockMap;
     }
 
     // 对ReferenceBean的getObject方法的Mock
     @SuppressWarnings ( "unchecked" )
     @Mock
     public  Object getObject(Invocation inv)  throws  Exception {
         ReferenceBean ref = inv.getInvokedInstance();
         String interfaceName = ref.getInterface();
         Object mock = mockMap.get(interfaceName);
         if  (mock !=  null ) {
             return  mock;
         }
         return  ( new  MockUp(Class.forName(interfaceName)) {
         }).getMockInstance();
     }
}

 


© JMockit中文网 2018  打赏咖啡

Guess you like

Origin www.cnblogs.com/funkboy/p/12012551.html