接口测试框架开发(一):rest-Assured_接口返回数据验证
转载:http://www.cnblogs.com/lin-123/p/7111034.html
返回的json数据:{"code":"200","message":"成功","content":{"orgiData":[{"customerName":"十堰市商汇小额贷款股份有限公司","customerId":211}],"queryType":2,"count":1,"resultCode":1}}
验证代码:
resp1.body("message", containsString("成功"));
resp1.body("content.resultCode", equalTo(1));
resp1.body("content.orgiData[0].customerId", equalTo(211));
详细代码:
package restAussuredGroup.restAussuredArtifact;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
import io.restassured.response.ValidatableResponse;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class NewPost {
@Test
public void f() {
ValidatableResponse resp1 = given()
.contentType("application/json")
.when()
.post("?jsonStr=eyJ0eXBlIjoiMiIsIm5hbWUiOiLljYHloLDluILllYbmsYflsI/pop3otLfmrL7ogqHku73mnInpmZDlhazlj7giLCJwYWdlU2l6ZSI6IjIwIiwicGFnZU51bSI6IjEifQ==&salt=10f6c0f77a3e34883307646822cb4c31")
.then();
resp1.assertThat().statusCode(200);
System.out.println(resp1.extract().asString());
resp1.body("message", containsString("成功"));
resp1.body("content.resultCode", equalTo(1));
resp1.body("content.orgiData[0].customerId", equalTo(211));
}
@BeforeTest
public void beforeTest() {
RestAssured.baseURI = "http://192.168.103.2";
RestAssured.port = 8580;
RestAssured.basePath = "/cd/rest/ybgcAppOrder/getOrg";
RestAssured.registerParser("text/plain", Parser.JSON);
}
@AfterTest
public void afterTest() {
}
}