Spring + Dubbo + TestNG interface test

Because the test needs of the project in May this year, came into contact with, a dubbo interface testing, record what they have learned as follows


In addition to the company's project http interfaces, some of the services involved in micro dubbo interface test, the current way of testing is to use Spring + Dubbo + TestNG-- dubbo implement RPC (calling function / method development projects in the test project), Spring framework using the various characteristics of test code to improve development efficiency, the use of flexible TestNG testing framework to write test code.


1 Introduction

Before the test you need to know some basic knowledge related to the relevant content can be found in the following link, this paper will not be repeatedly described:

[Knowledge]

Dubbo ●: Dubbo Introduction

TestNG ●: TestNG Tutorial

Testing the Spring ●: the Spring, the Spring and the Boot TestNG testing guidelines

●Mavan

● Spring binding TestNG up test environment: using Spring unit test

【business】

● dubbo interface documentation: measured according to the interface to send documents relevant for information interfaces need to be tested (the version number, the name of the interface & methods, interface functions)

● development code: if not detailed interface documentation, updates are not timely, direct view correspondence sent to test the branch, version of the code.

Next to a dubbo connector x projects, for example, to talk about the general process of the test.

FIG interface information exemplified as follows:

2, Dubbo service interfaces, test environment

First Spring + testNG premise and setting up the environment first, and then Dubbo interface testing environment set up:

1, is introduced in the pom.xml dependencies Dubbo

(Not described in detail, it is unclear can refer to the actual business side of the project calls dubbo interface in pom.xml)

2, Pom.xml introduction of the corresponding service application dependent jar:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<properties>

  <usercenter-api.version>1.0-SNAPSHOTuser-api.version>

</properties>

<dependencies>

 <dependency>

   <groupId>com.x</groupId>

   <artifactId>user-api</artifactId>

   <version>${user-api.version}</version>

 </dependency>

</dependencies>

</project>

 

3, Dubbo spring configuration service

Because the testing process is RPC (Remote Call Interface) process, a test project is equivalent to the service consumer Consumer, development projects equivalent to service providers Provider, so the test need only spring configuration of the consumer.

(1) We look at the service configuration service provider is like, (profile path provider / src / main / resources / spring / applicationContext-dubbo-provider.xml)

<description>基础配置</description>

<!-- 提供方应用信息,用于计算依赖关系 -->

<dubbo:application name="user"/>

<!-- 使用zookeeper注册中心暴露服务地址 -->

<dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}"/>

<!-- 用dubbo协议在任意一个没占用端口暴露服务 -->

<dubbo:protocol name="dubbo" port="-1" threads="XX"/>

<dubbo:provider id="user-provider" filter="logInputParamsFilter" timeout="2000"/>

<!-- 声明需要暴露的服务接口 -->

<dubbo:service interface="com.x.api.service.UserApiService" ref="userApiService"/>

<!-- 具体的实现bean -->

<bean id="userApiService" class="com.x.api.impl.UserApiServiceImpl"/>

 

(2) In our new project resources directory a xml file (applicationContext-dubbo-consumer.xml)

All you need to configure for each service:

<dubbo:application name="ucser-consumer"/> 

<dubbo:reference id="userApiService" interface="com.x.api.service.UserApiService"/>

 

(3) spring basic configuration Well, you can write the test scripts

package com.x.User;

import com.google.common.collect.Sets;

import com.x.api.service.UserApiService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;

import org.testng.annotations.Test;

@ContextConfiguration(locations = "classpath:spring/*.xml")

public class UserApiServiceTest extends AbstractTestNGSpringContextTests {

    @Autowired

    private UserApiService userApiService;

    @Test

    private void testgetUserInDepartment(){

        Set ids = userApiService.getUser("xxxxxxxxxxxxxxxxxxxxx");

        System.out.println(ids);}

}

 

In the above test code to note three points ::

Test class needs to inherit AbstractTestNGSpringContextTests, if you do not do this test class is unable to start Spring container

Use the [@ContextConfiguration] it is being tested in order to load Bean

Bean objects carried when the Spring framework dependency injection, use can @Autowired fields, methods and constructors are labeled, to complete the work of the automatic assembly.

(4) test case design

Test code may be tested for the parameters of the method the method returns & verify. For example, the function of this getUser (String Id) method is to "get the specified user id" parameter is the id, so the design of test points can also be considered Id is empty, the user has deleted, there is no abnormal situation such as id, with parameters @DataProvider based approach to testing (This article does not expand a description).

When testing to consider:

● interfaces deal with a variety of parameters are normal circumstances (including the interface has no correct abnormal thrown).

● can understand the needs of the business side, so as to determine the reasonableness of interface design on the business logic.

● impacting the interface to other interfaces measured (for example, update the interface type update the fields will not lead to other interfaces required data field is lost).

● MQ message corresponding to the interface call transmission is normal.

● time-consuming, real-time extension interface calls and other performance indicators.

Usually such a test can be found with a bug: null pointer exception, not taking into account the development of the scene, no treatment of abnormal mass participation number, delay rate abnormalities.

3 Summary

Above is to use Spring + Dubbo + TestNG way to test a brief dubbo interface.

4, expansion

jmeter to dubbo interface stress tests .

Published 11 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/sinat_16683257/article/details/82911490