>我有以下响应,我需要验证正文。
杰森 :
[
"Admin Login",
"Admin Contact",
"Administrator",
"Ads-View"
]
法典:
@Test(groups = {"ITTest"})
public void testAdmin()
{
com.jayway.restassured.response.Response testAdminResponse = get("/Admin");
testAdminResponse.then().assertThat().statusCode(200);
testAdminResponse.then().assertThat().body("$",equalTo("["Admin Login", "Admin Contact", "Administrator", "Ads-View"]"));
}
我尝试了Hamcrest匹配器 - equalTo,containsString,hasItems,hasItem,但都是徒劳的
如何完全验证身体?
如果您希望验证整个响应,并且您确定响应将采用以下格式
[ "管理员登录", "管理员联系人", "管理员", "广告视图" ]
然后,您可以将响应转换为字符串并使用 Assert.assertEquals
Assert.assertEquals(**response**, "[n" +
" "Admin Login",n" +
" "Admin Contact",n" +
" "Administrator",n" +
" "Ads-View"n" +
"]");
我在本地运行它,它对我来说工作正常
RestAssured.baseURI = "http://127.0.0.1:3000/Admin";
String **response** = RestAssured.given().
when().get().then().extract().asString();
Assert.assertEquals(response, "");
System.out.println("Body is : " + response);