???????????? 基于Spring開發的DUBBO服務接口測試
?
知識共享主要內容:
1、 Dubbo相關概念和架構,以及dubbo服務程序開發步驟。
2、 基于Spring開發框架的dubbo服務接口測試相關配置。
3、 spring test+junit和spring test+TestNG兩種測試框架腳本編寫方法。
?
一、??????? DUBBO與DUBBO架構
1、????????? 什么是dubbo?DUBBO是一個分布式服務框架,致力于提供高性能和透明化的RPC遠程服務調用方案,是阿里巴巴SOA服務化治理方案的核心框架,每天為2,000+個服務提供3,000,000,000+次訪問量支持,并被廣泛應用于阿里巴巴集團的各成員站點。
2、????????? DUBBO架構:
??????
二、??????? Dubbo服務程序開發過程(服務提供者,服務消費者,配置文件)
- 服務提供者:
1)?????? 定義服務接口
2)?????? 定義接口實現類
3)?????? Spring配置聲明暴露服務:
????
4)?????? 加載Spring配置
?????
- 服務消費者:
5)?????? Spring配置引用遠程服務
??????
6)?????? 加載Spring配置,并調用遠程服務
u? ClassPathXmlApplicationContext加載配置,然后用getBean方法獲取遠程代理。
?????
u? 用IOC注入:測試腳本是用這種方式的。
?
三、??????? Dubbo服務接口測試環境準備
1、??? POM.xml引入對應service應用jar依賴。
比如:
dependency>
??? <groupId>com.XXXX.basisdata</groupId>
??? <artifactId>basisdata-bankbill-common-facade</artifactId>
??? <version>1.1.0</version>
</dependency>
2、??? Dubbo服務spring配置
u? 由于測試過程是遠程調用接口的過程,所以只需要進行消費方spring配置。
u? 由于阿里云dubbo應用的測試環境屬于外網,本地機器需將請求通過公網機器的端口轉發給測試環境,需要在公網IPTable配置映射。
u? 沒有經過注冊中心,所以不用配置注冊中心。
Spring-dubbo配置文件只需對每個service如下配置:
<dubbo:reference interface="com.xxx.xxx.xxx.service.BillDetailService" id="billDetailService" url="dubbo://121.43.177.8:20100" timeout="10000"/>
然后在spring-context.xml加入引入資源配置即可。
<import resource="spring-secret.xml" />
?
四、??????? 腳本設計結構:
- 創建測試類公共父類,繼承AbstractTestNGSpringContextTests或者AbstractJUnit4SpringContextTests。
- 創建測試類,繼承父類,編寫相應代碼。
???????
五、??????? 腳本兩種基本編寫方法:
1、??? 繼承AbstractJUnit4SpringContextTests方法。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring-context.xml"})
@Configuration
public class BaseJunit4Test extends AbstractJUnit4SpringContextTests {
}
?
2、??? 繼承AbstractTestNGSpringContextTests方法。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring-context.xml"})
@Configuration
public class BaseTestNGTest extends AbstractTestNGSpringContextTests {
}
?????????????? 測試類繼承BaseTestNGTest即可。
六、??????? 數據驅動兩種基本編寫方法:
1、 基于Junit數據驅動。
u? 父類配置:
- @RunWith(Parameterized.class)
@ContextConfiguration(locations = {"classpath:/spring-context.xml"})
@Configuration
public class BaseJunit4Test extends AbstractJUnit4SpringContextTests {
??? protected TestContextManager testContextManager;
??? @Before
??? public void setUpContext() throws Exception {
??????? this.testContextManager = new TestContextManager(getClass());
??????? this.testContextManager.prepareTestInstance(this);
??? }
}
u? 接口測試類需編寫一個構造類和一個由@Parameterized.Parameters參數數據方法
@Parameterized.Parameters
public static Collection<Integer[]> getTestParameters(){
//
//??????? List<Integer[]> list = new ArrayList<Integer[]>();
//??????? list.add(new Integer[]{2000998248});? //expected,valueOne,valueTwo
//??????? list.add(new Integer[]{2000020021});
//??????? list.add(new Integer[]{2001999335});
//??????? String st=list.toString();
//??????? System.out.println("list值:" + st);
//??????? return list;
//??? }
List<Integer[]> list = new ArrayList<Integer[]>();
list? =Arrays.asList(new ??Integer[][]{{2000998248},{2000020021},{2001999335}});
return list;
}
- 構造方法:
- public TestSelectListByUserId2(Integer userid){
??? this.testUser = userid;
}
2、 基于TESTNG數據驅動。
u? 父類配置:
@ContextConfiguration(locations = {"classpath:/spring-context.xml"})
@Configuration
public class BaseTestNGTest extends AbstractTestNGSpringContextTests{
}
u? 測試接口類需加一個由@DataProvider(name = "集合標識")注解的數據收集的方法,并將@Test(dataProvider="集合標識")給需要用參數的測試方法。
數據收集方法:
@DataProvider(name = "testdata")
public Object[][] dataprovide()throws IOException{
System.out.println("dataprovide方法執行");
//??????? return new Object[][]{{2000020013,2},{2001000138,0},{2001000139,2}};
Object[][] testData =ExcelHandle.readXlsx(excel, "工作表2");
return testData;
}
u? 測試方法:
?????? @Test(dataProvider="testdata")
public void test_case_1(HashMap<String, String> map) throws Exception {
operatorUserId=Integer.valueOf(map.get("userId"));
exceptedvalue =Integer.valueOf(map.get("excepted"));
????????//++++++++++++++實際值+++++++++++++
Integer actual_value =
billService.getUserEmailNameCount(operatorUserId);
//預期值
Integer excepted_value =get_excepted_value(operatorUserId);
//++++++++++++++驗證+++++++++++++
Assert.assertEquals(actual_value,exceptedvalue);
}
?