快嘉sdkg使用手册(三) 代码生成实践 - fastjrun/sdkg GitHub Wiki
在快嘉sdkg使用手册(一)--小试牛刀中,我们下载了sdkg源码,本地仓库安装了快嘉sdkg-helper插件和快嘉single-sdk-archetype脚手架;接下来用single-sdk-archetype脚手架新建了一个test-sdk工程,并用sdkg-helper插件生成接口sdk代码和接口单元测试代码,然后用命令行直接执行了接口测试,输出测试报告。
在快嘉sdkg使用手册(二)--test-sdk实践指南中,我们演示了如何用eclipse调试test-sdk的testng测试用例,重点介绍了这些测试用例如何在eclipse执行,包括如何修改测试数据,如何在同一个单元测试用例执行多套测试数据;除此之外,我们也介绍了通过命令行复用test-sdk的测试用例对接口系统做轻量的性能测试和自动化测试。
当然这样的test-sdk在实际项目中还派不上用场,而这里我们也仅仅是把它当成脚手架而已,顺便讲了下test-sdk在支持单元测试、联调、性能测试和自动化测试方面的一些特性。接下来本节主要讲如何结合sdkg-helper利用test-sdk对指定接口系统进行对接,形成指定接口系统sdk,并能对指定接口系统做轻量的性能测试和自动化测试。
在这里我们以聚合数据的邮编查询的三个接口为例,这三个接口定义参见:邮编查询
备注:以下代码中用到的参数key需要读者自行配置,该参数只有注册为聚合数据用户,且开通邮编查询接口业务才能使用。
经过初步分析,我们认为聚合数据邮编查询的三个接口可以用快嘉框架预定义的通用接口类型genericControllers在sdk中dtype固定为“json”的时候可以支持,因此我们先将这三个接口定义维护在接口定义文件中,即test-sdk-client.xml中,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<bundle>
<packets>
<packet class="packet.api.QueryAddressByPostcodeResponseBody"
parent="com.fastjrun.packet.BaseResponseBody">
<field name="error_code" dataType="Integer" length="10"
canBeNull="false" remark="返回码" />
<object name="result"
class="packet.api.QueryAddressByPostcodeResponseBodyResult">
<list name="list"
class="packet.api.QueryAddressByPostcodeResponseBodyResultList">
<field name="PostNumber" dataType="String" length="6"
canBeNull="false" remark="邮编" />
<field name="Province" dataType="String" length="64"
canBeNull="false" remark="省份" />
<field name="City" dataType="String" length="64"
canBeNull="false" remark="城市" />
<field name="District" dataType="String" length="64"
canBeNull="false" remark="地区" />
<field name="Address" dataType="String" length="64"
canBeNull="false" remark="地址" />
</list>
</object>
</packet>
<packet class="packet.api.QueryPcdResponseBody"
parent="com.fastjrun.packet.BaseResponseBody">
<field name="error_code" dataType="Integer" length="10"
canBeNull="false" remark="返回码" />
<list name="result"
class="packet.api.QueryPcdResponseBodyResult">
<field name="id" dataType="String" length="10"
canBeNull="false" remark="省份ID" />
<field name="province" dataType="String" length="64"
canBeNull="false" remark="省份" />
<list name="city"
class="packet.api.QueryPcdResponseBodyResultCity">
<field name="id" dataType="String" length="10"
canBeNull="false" remark="城市ID" />
<field name="city" dataType="String" length="64"
canBeNull="false" remark="城市" />
<list name="district"
class="packet.api.QueryPcdResponseBodyResultCityDistrict">
<field name="id" dataType="String" length="10"
canBeNull="false" remark="区域ID" />
<field name="district" dataType="String" length="64"
canBeNull="false" remark="区域" />
</list>
</list>
</list>
</packet>
<packet class="packet.api.QueryPostcodeByAddressResponseBody"
parent="com.fastjrun.packet.BaseResponseBody">
<field name="error_code" dataType="Integer" length="10"
canBeNull="false" remark="返回码" />
<object name="result"
class="packet.api.QueryPostcodeByAddressResponseBodyResult">
<list name="list"
class="packet.api.QueryPostcodeByAddressResponseBodyResultList">
<field name="PostNumber" dataType="String" length="6"
canBeNull="false" remark="邮编" />
<field name="Province" dataType="String" length="64"
canBeNull="false" remark="省份" />
<field name="City" dataType="String" length="64"
canBeNull="false" remark="城市" />
<field name="District" dataType="String" length="64"
canBeNull="false" remark="地区" />
<field name="Address" dataType="String" length="64"
canBeNull="false" remark="地址" />
</list>
</object>
</packet>
</packets>
<genericControllers>
<genericController name="PostcodeController"
path="/postcode/" clientName="PostcodeClient"
clientParent="com.fastjrun.client.BaseGenericClient"
remark="邮编查询" tags="邮编查询">
<service name="postcodeService" class="PostcodeRest">
<method name="query" version="" path="query"
remark="根据相关条件查询符合条件地区的邮编" method="GET">
<parameters>
<parameter name="postcode" dataType="String" length="11"
canBeNull="false" remark="邮编,如:215001" />
<parameter name="key" dataType="String" length="64"
canBeNull="true" remark="应用APPKEY(应用详细页查询)" />
<parameter name="page" dataType="Integer" length="11"
canBeNull="false" remark="第几页,默认为第1页" />
<parameter name="pagesize" dataType="Integer"
length="11" canBeNull="false" remark="每页显示条数,(1-50)默认20条," />
<parameter name="dtype" dataType="String" length="10"
canBeNull="true" remark="返回数据的格式,xml或json,默认json" />
</parameters>
<response
class="packet.api.QueryAddressByPostcodeResponseBody" />
</method>
<method name="pcd" version="" path="pcd"
remark="返回省份、城市、地区(县)关联的列表">
<parameters>
<parameter name="key" dataType="String" length="64"
canBeNull="true" remark="应用APPKEY(应用详细页查询)" />
<parameter name="dtype" dataType="String" length="10"
canBeNull="true" remark="返回数据的格式,xml或json,默认json" />
</parameters>
<response
class="packet.api.QueryPcdResponseBody" />
</method>
<method name="search" version="" path="search.php"
remark="根据相关条件查询符合条件地区的邮编">
<parameters>
<parameter name="pid" dataType="Integer" length="11"
canBeNull="false" remark="省份ID" />
<parameter name="cid" dataType="Integer" length="11"
canBeNull="false" remark="城市ID" />
<parameter name="did" dataType="Integer" length="11"
canBeNull="false" remark="区域ID" />
<parameter name="q" dataType="String" length="11"
canBeNull="false" remark="地名关键字,如:木渎" />
<parameter name="page" dataType="Integer" length="11"
canBeNull="false" remark="第几页,默认为第1页" />
<parameter name="pagesize" dataType="Integer"
length="11" canBeNull="false" remark="每页显示条数,(1-50)默认20条," />
<parameter name="key" dataType="String" length="64"
canBeNull="true" remark="应用APPKEY(应用详细页查询)" />
<parameter name="dtype" dataType="String" length="10"
canBeNull="true" remark="返回数据的格式,xml或json,默认json" />
</parameters>
<response
class="packet.api.QueryPostcodeByAddressResponseBody" />
</method>
</service>
</genericController>
</genericControllers>
</bundle>
然后进入test-sdk目录,执行下面命令,重新生成代码
mvn clean package -Dsdkg.skip=false
src/main/resources/test-sdk-sdk.properties
test-sdk.genericUrlPre=${test-sdk.genericUrlPre}
pom.xml 中 test-sdk.genericUrlPre的值改为http://v.juhe.cn
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.share</groupId>
<version>1.1-SNAPSHOT</version>
<artifactId>test-sdk</artifactId>
<name>test-sdk</name>
<dependencies>
<dependency>
<groupId>com.fastjrun.share</groupId>
<artifactId>sdkg-common</artifactId>
<version>${sdkg.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>test-sdk-sdk.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>log4j2.xml</include>
<include>testdata/${envName}.properties</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<includes>
<include>testng.xml</include>
</includes>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>com.fastjrun.share</groupId>
<artifactId>sdkg-helper</artifactId>
<version>${sdkg.version}</version>
<executions>
<execution>
<id>initialize phase</id>
<phase>initialize</phase>
<goals>
<goal>sdkgc</goal>
</goals>
<configuration>
<bundleFiles>test-sdk-client.xml</bundleFiles>
<sdkDir>.</sdkDir>
<appName>test-sdk</appName>
<packagePrefix>com.alibaba.testsdk.</packagePrefix>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>log4j2.xml</exclude>
<exclude>test-sdk-sdk.properties</exclude>
<exclude>testdata/*.properties</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/main/otherjava</source>
<!-- 我们可以通过在这里添加多个source节点,来添加任意多个源文件夹 -->
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>compile</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
<testClassesDirectory>target/test-classes</testClassesDirectory>
<suiteXmlFiles>
<suiteXmlFile>target/test-classes/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>nocheck</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<skipTests>true</skipTests>
<checkstyle.skip>true</checkstyle.skip>
<license.skip>true</license.skip>
<notice.skip>true</notice.skip>
<versions.skip>true</versions.skip>
</properties>
</profile>
<!-- 测试需要带参数 -DskipTests=false -->
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- src/main/resources/test-sdk-sdk.properties -->
<test-sdk.genericUrlPre>http://v.juhe.cn</test-sdk.genericUrlPre>
<envName>local</envName>
</properties>
</profile>
</profiles>
<properties>
<sdkg.version>1.0-SNAPSHOT</sdkg.version>
<testng.version>6.10</testng.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
每次代码生成都会在test/data下重新生成相应的client的测试数据配置的格式信息,如上图所示,只需要按照数据类型设置好相应的参数就可以正常执行测试用例了,下面是我们的local.properties文件内容
PostcodeClient.pcd.1={"dtype"\:"json","key"\:"dca2ab6a470aadd432943755f1"}
PostcodeClient.search.1={"q"\:"\u8F66\u9053\u6C9F","pagesize"\:10,"dtype"\:"json","pid"\:4,"page"\:1,"did"\:1059,"key"\:"dca2ab6a470aadd432943755f1","cid"\:61}
PostcodeClient.query.1={"postcode"\:"100089","pagesize"\:"10","dtype"\:"json","page"\:"1","key"\:"dca2ab6a470aadd432943755f1"}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="clientTest" parallel="classes" thread-count="5"
data-provider-thread-count="20">
<test name="${envName}" parallel="methods"
thread-count="5">
<parameter name="envName" value="${envName}"></parameter>
<classes>
<class
name="com.alibaba.testsdk.client.PostcodeClientTest" />
</classes>
</test>
</suite>
本次调整代码参见 https://github.com/fastjrun/test-sdk/releases/tag/v1.1
重新分析聚合数据邮编查询的三个接口,我们发现它们会共用一个参数key,其次返回参数中均有error_code和reason参数,且多次观察返回结果为正常的时候error_code为0。据此我们抽象出一个新的抽象类com.alibaba.testsdk.client.BasePostcodeClient如下,该类文件我们放在src/main/otherjava目录下
package com.alibaba.testsdk.client;
import java.util.Map;
import java.util.ResourceBundle;
import com.fastjrun.client.BaseGenericClient;
import com.fastjrun.common.CodeException;
import net.sf.json.JSONObject;
/**
* 注意:邮编查询接口访问抽象类
*
* @Copyright 2018 快嘉框架. All rights reserved.
* @author cuiyingfeng
*/
public abstract class BasePostcodeClient extends BaseGenericClient {
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@Override
public void initSDKConfig(String apiworld) {
ResourceBundle rb = ResourceBundle.getBundle(apiworld + "-sdk");
this.genericUrlPre = rb.getString(apiworld + ".genericUrlPre");
this.key = rb.getString(apiworld + ".key");
}
@Override
protected JSONObject process(String reqStr, String urlReq, String method, Map<String, String> requestProperties) {
JSONObject responseJsonObject = this.processInternal(reqStr, urlReq + "&key=" + this.key, method,
requestProperties);
try {
int code = responseJsonObject.getInt("error_code");
if (code == 0) {
return responseJsonObject;
}
//这里可以过滤错误码,对指定错误码可以进行再封装
String msg = responseJsonObject.getString("reason");
if (msg == null) {
msg = "";
}
throw new CodeException(String.valueOf(code), msg);
} catch (Exception e) {
throw new CodeException("603", "返回数据error_code有误");
}
}
}
我们重写了initSDKConfig方法,可以为该类设置key;我们也重写了process方法,改写了请求路径,同时又在这里做了一道防火墙,可以对指定的错误码进行过滤。
新增com.alibaba.testsdk.packet.BasePostcodeResponseBody,作为所有响应报文的基类
package com.alibaba.testsdk.packet;
import com.fastjrun.packet.BaseResponseBody;
public abstract class BasePostcodeResponseBody extends BaseResponseBody {
/**
* 返回说明
*
*/
private String reason;
/**
* 返回码
*
*/
private Integer error_code;
public String getReason() {
return this.reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public Integer getError_code() {
return this.error_code;
}
public void setError_code(Integer error_code) {
this.error_code = error_code;
}
}
分析通过邮编查询对应的地名接口返回报文定义和根据相关条件查询符合条件地区的邮编接口返回报文定义一致,也就是说我们在接口定义文件中可以复用。
新的接口文件调整如下
<?xml version="1.0" encoding="UTF-8"?>
<bundle>
<packets>
<packet class="packet.postcode.QueryAddressByPostcodeResponseBody"
parent="com.alibaba.testsdk.packet.BasePostcodeResponseBody">
<object name="result"
class="packet.postcode.QueryAddressByPostcodeResponseBodyResult">
<list name="list"
class="packet.postcode.QueryAddressByPostcodeResponseBodyResultList">
<field name="PostNumber" dataType="String" length="6"
canBeNull="false" remark="邮编" />
<field name="Province" dataType="String" length="64"
canBeNull="false" remark="省份" />
<field name="City" dataType="String" length="64"
canBeNull="false" remark="城市" />
<field name="District" dataType="String" length="64"
canBeNull="false" remark="地区" />
<field name="Address" dataType="String" length="64"
canBeNull="false" remark="地址" />
</list>
</object>
</packet>
<packet class="packet.postcode.QueryPcdResponseBody"
parent="com.alibaba.testsdk.packet.BasePostcodeResponseBody">
<list name="result"
class="packet.postcode.QueryPcdResponseBodyResult">
<field name="id" dataType="String" length="10"
canBeNull="false" remark="省份ID" />
<field name="province" dataType="String" length="64"
canBeNull="false" remark="省份" />
<list name="city"
class="packet.postcode.QueryPcdResponseBodyResultCity">
<field name="id" dataType="String" length="10"
canBeNull="false" remark="城市ID" />
<field name="city" dataType="String" length="64"
canBeNull="false" remark="城市" />
<list name="district"
class="packet.postcode.QueryPcdResponseBodyResultCityDistrict">
<field name="id" dataType="String" length="10"
canBeNull="false" remark="区域ID" />
<field name="district" dataType="String" length="64"
canBeNull="false" remark="区域" />
</list>
</list>
</list>
</packet>
</packets>
<genericControllers>
<genericController name="PostcodeController"
path="/postcode/" clientName="PostcodeClient"
clientParent="com.alibaba.testsdk.client.BasePostcodeClient"
remark="邮编查询" tags="邮编查询">
<service name="postcodeService" class="PostcodeRest">
<method name="query" version="" path="query"
remark="通过邮编查询对应的地名" method="GET">
<parameters>
<parameter name="postcode" dataType="String" length="11"
canBeNull="false" remark="邮编,如:215001" />
<parameter name="page" dataType="Integer" length="11"
canBeNull="false" remark="第几页,默认为第1页" />
<parameter name="pagesize" dataType="Integer"
length="11" canBeNull="false" remark="每页显示条数,(1-50)默认20条," />
<parameter name="dtype" dataType="String" length="10"
canBeNull="true" remark="返回数据的格式,xml或json,默认json" />
</parameters>
<response
class="packet.postcode.QueryAddressByPostcodeResponseBody" />
</method>
<method name="pcd" version="" path="pcd"
remark="返回省份、城市、地区(县)关联的列表">
<parameters>
<parameter name="dtype" dataType="String" length="10"
canBeNull="true" remark="返回数据的格式,xml或json,默认json" />
</parameters>
<response
class="packet.postcode.QueryPcdResponseBody" />
</method>
<method name="search" version="" path="search.php"
remark="根据相关条件查询符合条件地区的邮编">
<parameters>
<parameter name="pid" dataType="Integer" length="11"
canBeNull="false" remark="省份ID" />
<parameter name="cid" dataType="Integer" length="11"
canBeNull="false" remark="城市ID" />
<parameter name="did" dataType="Integer" length="11"
canBeNull="false" remark="区域ID" />
<parameter name="q" dataType="String" length="11"
canBeNull="false" remark="地名关键字,如:木渎" />
<parameter name="page" dataType="Integer" length="11"
canBeNull="false" remark="第几页,默认为第1页" />
<parameter name="pagesize" dataType="Integer"
length="11" canBeNull="false" remark="每页显示条数,(1-50)默认20条," />
<parameter name="dtype" dataType="String" length="10"
canBeNull="true" remark="返回数据的格式,xml或json,默认json" />
</parameters>
<response
class="packet.postcode.QueryAddressByPostcodeResponseBody" />
</method>
</service>
</genericController>
</genericControllers>
</bundle>
然后进入test-sdk目录,执行下面命令,重新生成代码
mvn clean package -Dsdkg.skip=false
src/main/resources/test-sdk-sdk.properties
test-sdk.genericUrlPre=${test-sdk.genericUrlPre}
test-sdk.key=${test-sdk.key}
pom.xml 中增加test-sdk.key,其值设置为dca2ab6a470aadd432943755f1
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.share</groupId>
<version>1.1-SNAPSHOT</version>
<artifactId>test-sdk</artifactId>
<name>test-sdk</name>
<dependencies>
<dependency>
<groupId>com.fastjrun.share</groupId>
<artifactId>sdkg-common</artifactId>
<version>${sdkg.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>test-sdk-sdk.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>log4j2.xml</include>
<include>testdata/${envName}.properties</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<includes>
<include>testng.xml</include>
</includes>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>com.fastjrun.share</groupId>
<artifactId>sdkg-helper</artifactId>
<version>${sdkg.version}</version>
<executions>
<execution>
<id>initialize phase</id>
<phase>initialize</phase>
<goals>
<goal>sdkgc</goal>
</goals>
<configuration>
<bundleFiles>test-sdk-client.xml</bundleFiles>
<sdkDir>.</sdkDir>
<appName>test-sdk</appName>
<packagePrefix>com.alibaba.testsdk.</packagePrefix>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>log4j2.xml</exclude>
<exclude>test-sdk-sdk.properties</exclude>
<exclude>testdata/*.properties</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/main/otherjava</source>
<!-- 我们可以通过在这里添加多个source节点,来添加任意多个源文件夹 -->
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>compile</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
<testClassesDirectory>target/test-classes</testClassesDirectory>
<suiteXmlFiles>
<suiteXmlFile>target/test-classes/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>nocheck</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<skipTests>true</skipTests>
<checkstyle.skip>true</checkstyle.skip>
<license.skip>true</license.skip>
<notice.skip>true</notice.skip>
<versions.skip>true</versions.skip>
</properties>
</profile>
<!-- 测试需要带参数 -DskipTests=false -->
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- src/main/resources/test-sdk-sdk.properties -->
<test-sdk.genericUrlPre>http://v.juhe.cn</test-sdk.genericUrlPre>
<test-sdk.key>dca2ab6a470aadd432943755f1</test-sdk.key>
<envName>local</envName>
</properties>
</profile>
</profiles>
<properties>
<sdkg.version>1.0-SNAPSHOT</sdkg.version>
<testng.version>6.10</testng.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
每次代码生成都会在test/data下重新生成相应的client的测试数据配置的格式信息,如上图所示,只需要按照数据类型设置好相应的参数就可以正常执行测试用例了,下面是我们的local.properties文件内容
PostcodeClient.pcd.1={"dtype"\:"json"}
PostcodeClient.search.1={"q"\:"\u8F66\u9053\u6C9F","pagesize"\:10,"dtype"\:"json","pid"\:4,"page"\:1,"did"\:1059,"cid"\:61}
PostcodeClient.query.1={"postcode"\:"100089","pagesize"\:"10","dtype"\:"json","page"\:"1"}
本次调整代码参见 https://github.com/fastjrun/test-sdk/releases/tag/v1.2
#确认api-mock下的文件
tree /F
api-mock
│ pom.xml
│
└─src
└─main
└─otherjava
└─com
└─fastjrun
└─demo
FastjrunApplication.java
api-mock目录下只有两个文件及其组织关系如上所示。
备注:这里的test-sdk-client.xml指得是优化前的版本。
<plugin>
<groupId>com.fastjrun.share</groupId>
<artifactId>sdkg-helper</artifactId>
<version>${sdkg.version}</version>
<executions>
<execution>
<id>initialize phase</id>
<phase>initialize</phase>
<goals>
<goal>bundleMockgc</goal>
</goals>
<configuration>
<!--
<bundleFiles>../app-client.xml,../generic-client.xml,../api-client.xml</bundleFiles>
-->
<bundleFiles>test-sdk-client.xml</bundleFiles>
<bundleMockDir>.</bundleMockDir>
<packagePrefix>com.alibaba.testsdk.</packagePrefix>
</configuration>
</execution>
</executions>
</plugin>
如上插件的bundleFiles修改为test-sdk-client.xml
mvn clean package -Dbmgc.skip=false
java -jar target/api-mock.jar


