public interface GetIfc {
Response get(Map<String, String> headers, boolean isCookie, Map<String, String> params, String path);
}
public class BeforeTest {
@BeforeClass
public static void beforeClass() {
RestAssured.useRelaxedHTTPSValidation();
RestAssured.registerParser("text/html", Parser.JSON);//根据请求返回格式修改
RestAssured.baseURI = Utils.getTestData("baseURI");
// RestAssured.basePath = Utils.getTestData("basePath");
requestInfo(RestAssured.baseURI);
}
@Step
public static void requestInfo(String URL) {
//报告展现请求报文
}
}
通过@step定义一个方法,然后再请求模板中调用即可
public class GetImpl implements GetIfc {
@Override
public Response get(Map<String, String> headers, boolean isCookie, Map<String, String> params, String path) {
// qid 319203342
StringBuilder qt = new StringBuilder();
qt.append(Utils.getTestData("C"));
qt.append(Utils.getTestData("Q"));
String cookieStr = "";
if (isCookie == true) {
cookieStr = qt.toString();
}
if (headers == null) {
headers = new HashMap<>();
}
Response responseGet = given()
.headers(headers)
.params(params)
.cookie(cookieStr)
.log().all()
.get(path)
// .prettyPeek()
.then()
// .log().all()
.extract().response();
String requestHeader = RestAssured.baseURI;
String responseHeader = responseGet.getHeaders().toString();
//服务器响应
String StatusLine = RestAssured.get().getStatusLine();
responseGet.prettyPrint();//格式化参数
//断言
String json = responseGet.asString();
JsonPath jp = new JsonPath(json);
//测试报告展现 请求报文
requestHeader(requestHeader+path, params.toString());
//测试报告展现 响应报文
respondBody(StatusLine + "\r\n" + responseHeader, json);
return responseGet;
}
@Step
public static void requestHeader(String RequestHeader, String params) {
//报告展现请求报文
}
@Step
public void respondBody(String RespondHeader, String Respond) {
//报告展现响应报文
}
}
具体测试case
public class Buy extends BeforeTest {
@Test(description = "购买商品")
public void BuyTest() {
// String point = "1";
Map<String, String> params = new HashMap<>();
params.put("pid", pid);
params.put("round", round);
params.put("point", point);
params.put("token", "a2cb07a94d8ae8a95dbd72684c1b9cde");
Map<String, String> headers = new HashMap<>();
GetIfc get = new GetImpl();
Response response = get.get(headers, true, params, Utils.getTestData("buy_path"));
if (response.statusCode() == 200) {
Assert.assertEquals(response.jsonPath().get("errmsg"), "成功");
Assert.assertNotNull(response.jsonPath().get("data.total"));
Assert.assertEquals(response.jsonPath().get("data.point"), point);
} else {
Assert.fail("购买商品接口错误。");
}
}
}