最近因工作原因,需要测试dubbo接口,通过公司同事写的框架,再结合度娘的帮助,自己做了一些总结记录。

通过下文意在说明如何搭建一个spring + dubbo + testng的测试环境,并完成一个简单的dubbo接口测试用例。

一、在Idea中新建一个maven工程

二、在pom.xml中添加相关依赖

    <dependencies>
<!-- Spring框架依赖:用于构建Spring容器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> <!-- Spring单元测试依赖:用于测试类启动spring容器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
<scope>test</scope>
</dependency> <!-- dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency> <!-- 引入相应工程的jar包:*根据工程不同具体填入相应值-->
<dependency>
<groupId>com.*.*</groupId>
<artifactId>*</artifactId>
<version>*</version>
</dependency> <!-- testng依赖:测试框架-->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</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>
<scope>test</scope>
</dependency> </dependencies>

 三、Dubbo服务spring配置(resources目录下新建一个bean.xml文件)

由于测试过程是RPC(远程调用接口)的过程,测试项目相当于服务消费方Consumer,开发项目相当于服务提供方Provider,

所以测试只需要进行消费方的spring配置。xml的模板可以直接从要测试的dubbo接口对应的应用中拷贝;

<dubbo:application>必须声明,name和owner的值随便取(当然要取有一定意义的),没有对应关系;

<dubbo:reference>中url对应dubbo接口对应的dubbo服务地址和端口,

          id唯一即可,

          interface是dubbo服务的全限定类名

<?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"
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="x1_consumer" owner="x2_consumer"/>
<dubbo:reference
url="dubbo://192.168.0.100:20860"
id="testService"
interface="com.*.*.service.TestService"
timeout="30000"/> </beans>

四、编写测试脚本

import com.*.*.TestService;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import javax.annotation.Resource; //测试类需要继承AbstractTestNGSpringContextTests,如果不这么做测试类是无法启动Spring容器的
@ContextConfiguration(locations = "classpath:bean.xml")
public class TestBase extends AbstractTestNGSpringContextTests {
@Resource
public TestService testService; }

测试类需要继承AbstractTestNGSpringContextTests,如果不这么做测试类是无法启动Spring容器的

使用了[@ContextConfiguration]是为了加载被测试的Bean

import com.alibaba.fastjson.JSON;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; public class TestSaveData extends TestBase {
private TestDTO testDTO = new TestDTO();
private List<Integer> categorys = new ArrayList<Integer>(); @Test
public void TestSaveData_nameIsNull() {
categorys.add(1);
testDTO.setCategorys(categorys);
ResponseDTO res = TestService.saveData(testDTO);
System.out.println(JSON.toJSONString(res));
assertThat(res.getCode()).as("返回状态码").isEqualTo("2")
.isBetween("1","2");
assertThat(res.getMessage()).isEqualTo("XX名称不能为空!");
assertThat(res.isSuccess()).isFalse();
}
}

Spring+Dubbo+TestNG接口测试初探的更多相关文章

  1. 基于Spring开发的DUBBO服务接口测试

    基于Spring开发的DUBBO服务接口测试 知识共享主要内容: 1. Dubbo相关概念和架构,以及dubbo服务程序开发步骤. 2. 基于Spring开发框架的dubbo服务接口测试相关配置. 3 ...

  2. Spring Dubbo 开发笔记(一)——概述

    概述: Spring Dubbo 是我自己写的一个基于spring-boot和dubbo,目的是使用Spring boot的风格来使用dubbo.(即可以了解Spring boot的启动过程又可以学习 ...

  3. MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

    : 一:安装软件:tomcatZookeeperDubbo+admin 二:工程: 总工程  API    Pom.xml:不用引用任何东西  Provider    Pom.xml:要denpend ...

  4. Spring Dubbo 开发笔记

    第一节:概述 Spring-Dubbo 是我自己写的一个基于spring-boot和dubbo,目的是使用Spring boot的风格来使用dubbo.(即可以了解Spring boot的启动过程又可 ...

  5. java+testng接口测试入门

    testNG是一个测试框架,它能组织测试用例按照你想要的方式进行运行,并输出一定格式的便于阅读的测试报告(结果),通过java+testng的方式说明一下接口测试的基本使用方法. 一.环境搭建 a)千 ...

  6. spring+dubbo整合

    创建公共接口或者project用到的一些bean.我这里就仅仅是创建了一个接口.project文件夹例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...

  7. Spring+Dubbo集成Redis的两种解决方案

    当下我们的系统数据库压力都非常大,解决数据库的瓶颈问题势在必行,为了解决数据库的压力等需求,我们常用的是各种缓存,比如redis,本文就来简单讲解一下如何集成redis缓存存储,附github源码. ...

  8. 160719、Spring + Dubbo + zookeeper (linux) 框架搭建

    转载一篇博客,写得不错(至少我参考一下搭建成功了) 转载地址:http://my.oschina.net/wangt10/blog/522799 dubbo简介 节点角色说明: Provider: 暴 ...

  9. (转)Dubbo + Zookeeper入门初探

    一.搭建java和tomcat环境 二.搭建zookeeper 三.搭建dubbo监控中心 四.配置项目 4.1 服务提供方代码 4.2 服务使用方代码 五.测试 2018年2月15日,阿里巴巴的du ...

随机推荐

  1. 安装 KVM

    安装 KVM 需要的包 sudo apt-get install -y qemu-kvm qemu-system libvirt-bin virt-manager bridge-utils vlan ...

  2. SSH 连接时间超时

    linux服务端 # vi /etc/ssh/sshd_config ClientAliveInterval 60 ClientAliveCountMax 3 # 注: # ClientAliveIn ...

  3. Linux文件误删恢复

    一.需求研究 分析对比debugfs.testdisk 6.14.extundelete,对比各自官网介绍和操作说明本次决定研究extundelete对文件和目录的恢复操作. 二.项目内容 1.工具安 ...

  4. iOS 13 DeviceToken获取发生变化

    问题描述: iOS 13 通过[deviceToken description]获取到的内容已经变了,这段代码运行在 iOS 13 上已经无法获取到准确的DeviceToken字符串了, NSStri ...

  5. docker 使用阿里云镜像加速

    1.登录阿里云 2.进入控制台 3.搜索 “容器镜像服务” 下拉点击 “镜像加速器” 复制自己的私有地址 进入自己的docker宿主机器(替换下面的地址为自己的私有地址) 修改daemon配置文件/e ...

  6. Linux如何查看进程、杀死进程、启动进程

    1.查看进程:ps命令 下面的命令还没实践,仅仅供你参考:可以用man ps查看格式,只不过是一个小工具而已! ps a 显示现行终端机下的所有程序,包括其他用户的程序.    ps -A 显示所有程 ...

  7. WiFi、ZigBee、BLE用哪个?

    小米是这么选的: 1) 插电的设备,用WiFi: 2) 需要和手机交互的,用BLE: 3) 传感器用ZigBee. WIFI,WIFI是目前应用最广泛的无线通信技术,传输距离在100-300M,速率可 ...

  8. todo---git 生成密钥 原理分析

    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRJkDZ2z7syFC2QDCaORKF41ecwbL/kyFwkycOVE3MavTRBliAhoAhOaZQTr4j ...

  9. synchronized的不足与redis分布式锁的使用

    这里是一个简单模拟秒杀的逻辑,stock和orders为两个Map,分别模拟库存表和订单表 public void orderProductMockDiffUser(String productId) ...

  10. go 食用指南

    Golang高效食用秘籍 一.关于构建 1.1 go环境变量 $ go env // 查看go 的环境变量 其中 GOROOT 是golang 的安装路径 GOPATH 是go命令依赖的一个环境变量 ...