静态导入方法
静态导入如下方法,提高rest-assured使用效率:
io.restassured.RestAssured.*
io.restassured.matcher.RestAssuredMatchers.*
org.hamcrest.Matchers.*
使用Json Schema Validation应导入:
io.restassured.module.jsv.JsonSchemaValidator.*
在SpringMVC中,可以使用spring-mock-mvc
模型的Rest Assured DSL
来对Spring的controller层进行单元测试,为此需要从RestAssuredMockMvc
静态导入这些方法,而不是io.restassured.RestAssured
:io.restassured.module.mockmvc.RestAssuredMockMvc.*
示例一:JSON
某个get请求(http://localhost:8080/lotto
)返回JSON。REST assured可以进行get请求并对响应信息进行处理。
// 判断lottoId是否等于5:
get("/lotto").then().body("lotto.lottoId", equalTo(5));
// 判断winnerId的取值是否包含23和54:
get("/lotto").then().body("lotto.winners.winnerId", hasItems(23, 54));
// 以BigDecimal返回float和double类型数据
given().
config(RestAssured.config().jsonConfig(jsonConfig().numberReturnType(BIG_DECIMAL))).
when().
get("/price").
then().
body("price", is(new BigDecimal(12.12));
示例二:匿名式的JSON根节点验证
如[1, 2, 3]这样的匿名JSON的根属性可以使用$
或者空字符串
识别:
when().
get("/json").
then().
body("$", hasItems(1, 2, 3));