就是这么简单!使用Rest-assured 测试Restful Web Services
使用 Rest-assured 测试 Restful Web Services
转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html
这里向大家介绍一个测试Restful web service 的框架,叫Rest-assured.
他提供了一系列好的功能,像DSL式的语法, XPath-Validate, 文件上传,Specification重用, 使用代理, Spring MVC mock module测试Controllers等等,让你在Java里面测试Rest service 和那些动态语言Ruby, Groovy一样灵活。
目录
1. 前提
2. 配置
3. Example详解
4. Troubleshooting
5. 参考来源
前提条件
- JDK >= 1.6
- Maven 3
配置Maven工程pom文件如下
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.3.3</version>
<scope>test</scope>
</dependency><dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
Example
a) 测试一个GET 请求方法,
请求URL : http://10.46.28.193:8080/service/v1/user/login
返回JSON内容如下
{
"userInfo": {
"password": null,
"userId": "wadexu",
"accessSecurityCodes": "10000000000000000000",
"firstName": "Wade",
"lastName": "Xu",
"status": 8,
"officePhone": "58730",
"email": "wadexu@test.com",
"homePhone": "123"
},
"success": true,
"error": null
}
测试代码如下:
@Before
public void setUp() {
RestAssured.baseURI= "http://10.46.28.193";
RestAssured.port = 8080;
RestAssured.basePath = "/service/v1";
} @Test
public void testUserLogin() {
expect().
statusCode(200).
body(
"success", equalTo(true),
"userInfo.userId", equalTo("wadexu"),
"userInfo.firstName", equalTo("Wade"),
"userInfo.lastName", equalTo("Xu"),
"error", equalTo(null)).
when().
get("/user/login?userName=wadexu&password=NzrmRcIfIW4=");
}
注意我这里请求时的参数直接塞进了URL里, 稍后会讲到如何指明参数。
b) 如何使用JSON path
还是同上面的例子, 测试代码如下:
@Test
public void testUserLogin_JsonPath() {
Response response = get("/user/login?userName=wadexu&password=NzrmRcIfIW4=");
assertEquals(200, response.getStatusCode());
String json = response.asString();
JsonPath jp = new JsonPath(json);
assertEquals("wadexu", jp.get("userInfo.userId"));
assertEquals("Wade", jp.get("userInfo.firstName"));
assertEquals("Xu", jp.get("userInfo.lastName"));
assertEquals("123", jp.get("userInfo.homePhone"));
}
c) 如何使用参数
Get请求是用queryParam, 如果你直接写param,在这个case里也可以,Rest Assured 会自动判断参数类型(query or form parameter), 在有些case里, Put 或 Post 你得指明参数类型
@Test
public void testUserLogin_Parameter() {
final String userName = "wadexu";
final String password = "NzrmRcIfIW4="; given().
queryParam("userName", userName).queryParam("password", password).
expect().
statusCode(200).
body("success", equalTo(true),
"userInfo.userId", equalTo("wadexu"),
"userInfo.firstName", equalTo("Wade"),
"userInfo.lastName", equalTo("Xu"),
"error", equalTo(null)).when()
.get("/user/login");
}
另外,有些Post 请求URL后面是有参数的, 这时候 你可以这样写
post("/reserve/{hotelId}/{roomNumber}", "My Hotel", 23);
或者
given().
pathParam("hotelId", "My Hotel").
pathParam("roomNumber", 23).
when().
post("/reserve/{hotelId}/{roomNumber}").
then().
..
d) 再来看一个POST 请求, 这时候需要请求消息体body了,request body是JSON体如下:
{
"customerId": "CDICC",
"broker": "test",
"editUserId": "wadexu"
}
测试代码:
@Test
public void testCreate() {
final String bodyString = "{\"customerId\": \"CDICC\",\"broker\": \"test\",\"editUserId\": \"wadexu\"}"; given().
contentType("application/json").
request().body(bodyString).
expect().
statusCode(200).
body(
"order.orderNumber", is(Number.class),
"order.deleteDate", is(nullValue()),
"success", equalTo(true)).
when().
post("/order");
}
这时除了用到request().body
还多加了一个header 请求消息头 -- ContentType
set Headers 的方法有很多, 上面是其一, 你还可以按如下方式做:
given().header("Content-Type", "application/json")
given().headers("Accept", "application/json", "Content-Type", "application/json")
另外 注意到期望结果的比较没有, 这里用到org.hamcrest.Matchers的一些方法, 因为Order number 每次不一样,无法判断具体是多少,所以就看是否是数字就行了,删除日期是null value
hamcrest.Matchers 里的各种匹配器有兴趣的童鞋可以研究下, 对测试断言很有帮助。
转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html
e) 同样你还可以verify HTTP Status code
因为我这个service是需要Content-Type=application/json的, 而我的case里并没有赋值给contentType, 所以返回会报错 415
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
@Test
public void testOpenOrder_error() {
final String orderNumber = "3017";
final String orderVersion = "1";
final String versionType = "";
final String editUserId = "";
final String customerId = "";
final String state = ""; given().
parameters(
"orderNumber", orderNumber,
"orderVersion", orderVersion,
"versionType", versionType,
"editUserId", editUserId,
"customerId", customerId,
"state", state).
expect().
statusCode().
when().
post("/order/open");
}
f) Cookies 其实都大同小异了
第一个没有set cookie 结果抛 403
"name":"Forbidden",
"detail":"The request was a legal request, but the server is refusing to respond to it. Unlike a 401 Unauthorized response, authenticating will make no difference."
@Test
public void testCookie() {
expect().
statusCode().
when().
get("/access"); given().
cookie("userName", "wadexu").
expect().
statusCode(200).
when().
get("/access");
}
g) Authentication
如果你的service需要认证,则需要设置authentication()
否则401 -- Unauthorized
@Test
public void testAuthentication() {
expect().
statusCode().
when().
get("/service/user"); expect().
statusCode(200).
when().
with().
authentication().basic("wadexu", "123456").
get("/service/user");
}
H) Specification reuse 规范重用
@Test
public void testSpecReuse() { ResponseSpecBuilder builder = new ResponseSpecBuilder();
builder.expectStatusCode(200);
builder.expectBody("userInfo.userId", equalTo("wadexu"));
builder.expectBody("userInfo.firstName", equalTo("Wade"));
builder.expectBody("userInfo.lastName", equalTo("Xu"));
builder.expectBody("success", equalTo(true));
ResponseSpecification responseSpec = builder.build(); //use this specification for test example -- a
expect().
spec(responseSpec).
when().
get("/user/login?userName=wadexu&password=NzrmRcIfIW4="); //now re-use for another example -- c that returns similar data
given().
queryParam("userName", "wadexu").
queryParam("password", "NzrmRcIfIW4=").
expect().
spec(responseSpec).
when().
get("/user/login");
}
如果你还有更多的测试,返回期望结果又类似 则可以继续使用 specification, 达到重用的目的。
转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html
测试运行结果如下(不包含上面每一个用例):
Troubleshooting
有些类需要Static imports
参考我的如下:
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*; import com.jayway.restassured.RestAssured;
import com.jayway.restassured.builder.ResponseSpecBuilder;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.ResponseSpecification; import static com.jayway.restassured.RestAssured.*; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.*;
设置好你的请求url 路径, 默认http://localhost:8080
参考我的base path(即所以请求url 前面相同的部分) 配置如下:
@Before
public void setUp() {
RestAssured.baseURI= "http://10.46.28.193";
RestAssured.port = 8080;
RestAssured.basePath = "/service/v1";
}
“WARNING: Cannot find parser for content-type: text/json — using default parser.”
– 需要注册相关的parser: e.g. RestAssured.registerParser(“text/json”, Parser.JSON);
参考来源
官方文档:https://code.google.com/p/rest-assured/
羊年第一篇文章,感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以点击右下方的推荐按钮。您的鼓励是我创作的动力,祝大家羊年工作生活各方面洋洋得意!
就是这么简单!使用Rest-assured 测试Restful Web Services的更多相关文章
- RESTful Web Services测试工具推荐
命令行控的最爱:cURL cURL是一个很强大的支持各种协议的文件传输工具,用它来进行RESTful Web Services的测试简直是小菜一碟.这个工具基本上类Unix操作系统(各种Linux.M ...
- RESTful Web Services简单介绍
近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTful风格的Web Services,比较著名的包括Twit ...
- .NET RESTful Web Services入门
很早之前看到过RESTful Web Services,并未在意,也没找相关资料进行学习.今天偶尔有一机会,就找了点资料进行研究,发现RESTful真是“简约而不简单”.下面用示例来说明: 1 项目结 ...
- 使用 Spring 3 来创建 RESTful Web Services
来源于:https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/ 在 Java™ 中,您可以使用以下几种方法来创建 RESTful We ...
- 使用 Spring 3 来创建 RESTful Web Services(转)
使用 Spring 3 来创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参 ...
- 基于Spring设计并实现RESTful Web Services(转)
基于Spring设计并实现RESTful Web Services 在本教程中,你将会使用Spring来创建一个具有生产力的RESTful网络服务. 为什么用RESTful网络服务? 从和Amazon ...
- Spring 3 来创建 RESTful Web Services
Spring 3 创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参考实现 ...
- RESTful Web Services初探
RESTful Web Services初探 作者:杜刚 近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTf ...
- 【转】RESTful Web Services初探
近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTful风格的Web Services,比较著名的包括Twit ...
随机推荐
- CTSC&&APIO 2015 酱油记
在北京待了一周多,还是写点记录吧. 人民大学校园还是挺不错的,不过伙食差评. CTSC的题目太神,根本不会搞,一试20二试10分..本来都寄希望于提交答案题的..结果就悲剧了. 然后是听大爷们的论文答 ...
- Dooioo Deal
using AnfleCrawler.Common; using System; using System.Collections.Generic; using System.Linq; using ...
- 分析器错误 MvcApplication 找不到
<%@ Application Codebehind="Global.asax.cs" Inherits="test.MvcApplication" La ...
- HDU 3709 Balanced Number
发现只要Σa[i]*i%Σa[i]==0就可以. #include<iostream> #include<cstdio> #include<cstring> #in ...
- 进程间通信IPC:消息队列,信号量,共享内存
2015.3.4星期三 阴天 进程间通信:IPC 文件对象:记录文件描述符,文件开关等 IPC标示符:系统全局的流水号两个进程要通信,打开的是唯一的对象进行通讯,通过key操作 XSI IPC:消息队 ...
- SendInput模拟Win(VK_LWIN)键的问题
使用SendInput模拟按键,代码如下: #include "stdafx.h" #include <windows.h> #include <conio.h& ...
- python学习:函数的学习
我们写东东的时候,往往有一些东西要频繁去复用,那么每个功能是10行代码,复用2次就是20行,这样看来我们的程序如果频繁利用某些代码的话,那么会是我们开发的东西越来越臃肿.那么好的方法有没有呢,那就是函 ...
- Python 基礎 - 用戶交互程序
現在就來寫一個簡單的 用戶輸入 的程式,這是一個互動模式,需要使用者自已輸入 #!/usr/bin/env python3 # -*- coding:utf-8 -*- username = inpu ...
- Erlang 102 Erlang并发编程
笔记系列 Erlang环境和顺序编程Erlang并发编程Erlang分布式编程YawsErlang/OTP 日期 变更说明 2014-11-02 A outline 2014 ...
- ThinkPHP整合微信支付之发裂变红包
1.去商户平台里,给你的商户充钱,没钱是发不了红包哒! 2.微信红包需要证书支持,所以请大家到商户平台下去下载好证书后放到安全文件夹下,并且需要在配置文件中指定好证书路径! 好,接下来带来裂变红包具体 ...