java +testNG+dubbo接口测试框架搭建(一)
一.创建maven工程
1.打开IDEA,选择file-->new project,选择maven,选择自己本地的JDK,点击next:
建好以后,默认有src->main->java、resourcel两个文件夹,以及pom.xml文件
二、在pom.xml文件中导入依赖包
<groupId>com.iflytek.test</groupId>
<artifactId>QM-QQZW20-TEST</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 私有仓库依赖:maven仓库-->
<repositories>
<repository>
<id>repo</id>
<name>repo</name>
<url>http://maven.ifly.com:8888/nexus/content/groups/public</url>
</repository>
<repository>
<id>iflytek-public</id>
<name>iflytek-public</name>
<url>http://maven.ifly.com:8888/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies> <!-- Spring框架依赖:用于构建Spring容器 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.16.RELEASE</version> </dependency> <!-- Spring单元测试依赖:用于测试类启动spring容器 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.6.RELEASE</version> <!--注意去掉scode- </dependency> <!-- Spring其他依赖--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.6.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.3.16.RELEASE</version> </dependency>
<!-- dubbo依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency>
<!--EPSA DUBBO依赖,和dubbo二选一,根据项目来-->
<dependency> <groupId>com.iflytek.edu</groupId> <artifactId>epas-dubbo</artifactId> <version>1.0.4.1001</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency>
<!-- 引入相应工程的jar包:*根据实际项目去引入-->
<dependency> <groupId>xxxxx(e.g:com.qiming.qqzw)</groupId>
<artifactId>xxxxxx(e.g:resource-center-service-api)</artifactId>
<version>1.0.0.1001-SNAPSHOT</version>
</dependency>
<!-- testng依赖:测试框架-->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<!--注意去掉scode-->
</dependency>
<!-- fastjson依赖:主要用于接口返回结果中解析json对象 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<!-- 流式断言器依赖:要比于junit和testng中的断言,assertj的api更强大-->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
</dependency>
<!--引入@case注解-->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.2.3-SNAPSHOT</version>
</dependency>
<!--引入AbstractTestNGSpringContextTest-->
<dependency>
<groupId>com.github.yongchristophertang</groupId>
<artifactId>rest-connector</artifactId>
<version>0.5.17</version>
</dependency>
<!--引入其他依赖包-->
<!--引入git关联依赖-->
<dependency>
<groupId>com.github.yongchristophertang</groupId>
<artifactId>rest-connector</artifactId>
<version>0.5.17</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.2</version>
</dependency>
<!--其他依赖包结束-->
</dependencies>
<!--自动编译依赖-->
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/main/java</testSourceDirectory>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<!-- <testNGArtifactName>org.testng:testng</testNGArtifactName>-->
<forkMode>once</forkMode>
<!--<skipTests>true</skipTests>-->
</configuration>
</plugin>
</plugins>
</build>
三、配置xml配置文件,在resources文件下建bean.xml:
(1).dubbo配置bean.xml格式数据:
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="qzw20-test" owner="x2_consumer"/>
<!--填写提供者地址以及端口-->
<dubbo:registry address="zookeeper://110.1.1.1:8888" register="true"/>
<!--填写dubbo类,id值可以任意取,与interface没有联系-->
<dubbo:reference id="hanyuOverSeaSceneService" interface="com.qiming.qqzw.resource.api.oversea.IHanyuOverSeaSceneService" check="false"/>
(2)epas dubbo 配置bean.xml格式数据:
<?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://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- epas配置 客户端使用EpasConfig配置 --> <bean id="epasConfig" class="com.iflytek.edu.epas.dubbo.config.EpasConfig"> <!-- 在平台申请的appKey --> <property name="appKey" value="qqzw-code-test" /> <property name="appSecret" value="test" /> <!-- 地址服务url --> <property name="addrServerUrl" value="http://xxx.xxx.com/xxx" /> <!-- 是否默认使用dubbo代理服务(外网客户端模式) 默认值为false --> <!--<property name="proxy" value="false" />--> </bean> <dubbo:application name="qqzw20-test"/>
<!--group需要与开发确认,id随意填写,registry要与id对应-->
<dubbo:registry id="resourceConsumer" protocol="epas" address="epasConfig" group="qqzw-resource-service-test"/>
<dubbo:reference registry="resourceConsumer" id="hanyuBaseResourceService" interface="com.qiming.qqzw.resource.api.baseResource.IHanyuBaseResourceService" check="false"/>
四、编写接口测试脚本,新建一个文件TestApply,file->new ->java class:
import com.alibaba.fastjson.JSONObject; import com.github.yongchristophertang.database.testng.TestNGDBInjectionModuleFactory; import com.github.yongchristophertang.engine.java.ProxyFactories; import com.qiming.qqzw.resource.api.oversea.IHanyuOverSeaSceneService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.BeforeClass; import org.testng.annotations.Guice; import org.testng.annotations.Test; import org.uncommons.reportng.annotation.Case; import javax.annotation.Resource; import java.util.*; import java.util.Map; @Guice(moduleFactory = TestNGDBInjectionModuleFactory.class) @ContextConfiguration(locations = "classpath:bean*.xml")
//要注入Service @Service
public class TestApply extends AbstractTestNGSpringContextTests{ @Autowired private IHanyuOverSeaSceneService hanyuOverSeaSceneServiceProxy; private IHanyuOverSeaSceneService hanyuOverSeaSceneService; @BeforeClass public void init() { hanyuOverSeaSceneService = ProxyFactories.createLoggerProxy(hanyuOverSeaSceneServiceProxy); } @Test(priority=1) @Case("BVT test") public void pageOverseaScene_BVT_Test() { Map<String, Object> listOverseaSceneDialogueBySceneId_param = new HashMap<String, Object>(); listOverseaSceneDialogueBySceneId_param.put("pageNum",2); listOverseaSceneDialogueBySceneId_param.put("pageSize",6); JSONObject listResourceById_result= hanyuOverSeaSceneService.pageOverseaScene(listOverseaSceneDialogueBySceneId_param); } }