Spring+Dubbo+TestNG接口测试初探
最近因工作原因,需要测试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接口测试初探的更多相关文章
- 基于Spring开发的DUBBO服务接口测试
基于Spring开发的DUBBO服务接口测试 知识共享主要内容: 1. Dubbo相关概念和架构,以及dubbo服务程序开发步骤. 2. 基于Spring开发框架的dubbo服务接口测试相关配置. 3 ...
- Spring Dubbo 开发笔记(一)——概述
概述: Spring Dubbo 是我自己写的一个基于spring-boot和dubbo,目的是使用Spring boot的风格来使用dubbo.(即可以了解Spring boot的启动过程又可以学习 ...
- MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建
: 一:安装软件:tomcatZookeeperDubbo+admin 二:工程: 总工程 API Pom.xml:不用引用任何东西 Provider Pom.xml:要denpend ...
- Spring Dubbo 开发笔记
第一节:概述 Spring-Dubbo 是我自己写的一个基于spring-boot和dubbo,目的是使用Spring boot的风格来使用dubbo.(即可以了解Spring boot的启动过程又可 ...
- java+testng接口测试入门
testNG是一个测试框架,它能组织测试用例按照你想要的方式进行运行,并输出一定格式的便于阅读的测试报告(结果),通过java+testng的方式说明一下接口测试的基本使用方法. 一.环境搭建 a)千 ...
- spring+dubbo整合
创建公共接口或者project用到的一些bean.我这里就仅仅是创建了一个接口.project文件夹例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- Spring+Dubbo集成Redis的两种解决方案
当下我们的系统数据库压力都非常大,解决数据库的瓶颈问题势在必行,为了解决数据库的压力等需求,我们常用的是各种缓存,比如redis,本文就来简单讲解一下如何集成redis缓存存储,附github源码. ...
- 160719、Spring + Dubbo + zookeeper (linux) 框架搭建
转载一篇博客,写得不错(至少我参考一下搭建成功了) 转载地址:http://my.oschina.net/wangt10/blog/522799 dubbo简介 节点角色说明: Provider: 暴 ...
- (转)Dubbo + Zookeeper入门初探
一.搭建java和tomcat环境 二.搭建zookeeper 三.搭建dubbo监控中心 四.配置项目 4.1 服务提供方代码 4.2 服务使用方代码 五.测试 2018年2月15日,阿里巴巴的du ...
随机推荐
- Spring Cloud简介 4.1
什么是Spring Cloud Spring Cloud是在Spring Boot的基础上构建的,用于简化分布式系统构建的工具集.该工具集为微服务架构中所涉及的配置管理.服务发现.智能路由.断路器.微 ...
- Python - Django - FBV 和 CBV
FBV: Function Base View,基于函数的视图 views.py: from django.shortcuts import render, HttpResponse # FBV de ...
- delphi self.Update 什么作用
更新指定窗口的客户区.如果窗口更新的区域不为空,UpdateWindow函数就发送一个WM_PAINT消息来更新指定窗口的客户区.函数绕过应用程序的消息队列,直接发送WM_PAINT消息给指定窗口的窗 ...
- 【err】开启Persistence-M模式-Check failed: err == CUBLAS_STATUS_SUCCESS (1 vs. 0) : Create cublas handle failed
前言 安装好CUDA.CUDNN.NVIDIA driver之后,使用mxnet框架的时候出现该错误,本文记录该问题的解决方法. 环境 ubuntu 16.04 MxNet Cuda9.0 Nvidi ...
- 触屏Tap模拟事件
触屏的click因为有双击判断所以有200ms的延迟,zepto里的touch.js兼容不好所以tap也没法直接用. gibhub上有个fastclick太大了. 自己用touched写个简单的模拟t ...
- 【CSS3练习】transform 2d变形实例练习
transform 2d变形实例练习:练习了旋转 倾斜 缩放的功能 <!DOCTYPE html> <html lang="en"> <head> ...
- Mathtype安装与最简破解
1.MathType资源 链接: https://pan.baidu.com/s/1UapJCcfU7Me_rIWdAe5nfw 提取码: 1y9i 2.破解 我之前的30天试用期过了,没来得 ...
- Vue简单基础 + 实例 及 组件通信
vue的双向绑定原理:Object.defineProperty() vue实现数据双向绑定主要是:采用数据劫持结合发布者-订阅者模式的方式,通过 Object.defineProperty() 来劫 ...
- 记录 centos samba 安装
https://www.ruletree.club/archives/4/ https://www.linuxidc.com/Linux/2017-03/141390.htm
- C#中HttpWebRequest、WebClient、HttpClient的使用
HttpWebRequest: 命名空间: System.Net,这是.NET创建者最初开发用于使用HTTP请求的标准类.使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如 ...