Spring+Dubbo+TestNG介面測試
由於測試專案需要,今年五月開始接觸、進行dubbo介面測試,記錄所學如下
公司中的專案除http介面外,一些微服務專案中涉及dubbo介面測試,目前測試的方式是Spring+Dubbo+TestNG——利用dubbo實現RPC(在測試專案中呼叫開發專案提供的函式/方法),利用Spring Framework的各種特性提高測試程式碼的開發效率,使用TestNG測試框架靈活地編寫測試程式碼。
1、前言
測試前你需要先了解一些相關的基礎知識,相關內容可參見以下連結,本文不再冗述:
【知識點】
●Mavan
●Spring結合TestNG搭建測試環境:使用 Spring 進行單元測試
【業務】
●dubbo介面文件:根據送測相關的介面文件,瞭解需要測試的介面的資訊(版本號、介面&方法名、介面功能)
●開發程式碼:若介面文件不詳細、更新不及時,直接檢視對應送測分支、版本的程式碼。
接下來以x專案的一個dubbo介面為例,說說測試的大致流程。
舉例的介面資訊如下圖:
2、Dubbo服務介面測試環境搭建
前提先將Spring+testNG環境搭好先,再進行Dubbo介面測試環境搭建:
1、在pom.xml引入Dubbo依賴包
(不詳細描述,不清楚可以參考呼叫dubbo介面的實際業務方專案中的pom.xml)
2、Pom.xml引入對應service應用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配置
由於測試過程是RPC(遠端呼叫介面)的過程,測試專案相當於服務消費方Consumer,開發專案相當於服務提供方Provider,所以測試只需要進行消費方的spring配置。
(1) 我們先看一下服務提供方的service配置是怎樣的,(配置檔案路徑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)我們在工程resources目錄下新建一個xml檔案(applicationContext-dubbo-consumer.xml)
只需要對每個service進行如下配置:
<dubbo:application name="ucser-consumer"/>
<dubbo:reference id="userApiService" interface="com.x.api.service.UserApiService"/>
(3)spring基本配置好了,可以進行測試指令碼的編寫了
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);}
}
在上面的測試程式碼裡要注意三點::
測試類需要繼承AbstractTestNGSpringContextTests,如果不這麼做測試類是無法啟動Spring容器的
使用了[@ContextConfiguration]是為了載入被測試的Bean
在Spring框架進行bean物件依賴注入時,利用@Autowired可以對成員變數、方法和建構函式進行標註,來完成自動裝配的工作。
(4)測試用例的設計
測試程式碼裡可以對方法的引數進行測試&對方法返回進行校驗。比如這個getUser(String Id)方法的功能是“獲取指定id的使用者",引數是id,那麼設計測試點時也可以考慮Id為空、使用者已刪除、不存在id等異常情況,用@DataProvider引數化的方法來進行測試(本文不做展開說明)。
測試時要考慮:
●介面是否正常處理各種引數情況(包括介面異常有沒有正確丟擲)。
●可瞭解業務方的需求,進而判斷介面設計在業務邏輯上的合理性。
●所測介面對其他介面的影響(比如update型別介面更新的欄位不會導致其他介面需要的資料欄位丟失)。
●介面呼叫時的對應的MQ訊息傳送是否正常 。
●介面呼叫的耗時,即時延等效能指標。
一般如此測試可發現的bug有:空指標異常、未被開發考慮到的場景、未作處理的一些異常傳參、時延率異常等。
3、小結
以上就是用Spring+Dubbo+TestNG方式來測試dubbo介面的一個簡單介紹。