放心 - 响应主体验证

Rest Assured - Response body verification

我有以下回复,我需要验证正文。

Json :

[
   "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,但都是徒劳的

如何完全验证正文?

如果您希望从整体上验证响应,并且确定响应将采用以下格式

[ "Admin Login", "Admin Contact", "Administrator", "Ads-View" ]

然后您可以将响应转换为字符串并使用 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);