放心-响应机构验证

时间:2018-09-10 17:47:30

标签: rest-assured hamcrest

我的回答如下,我需要验证身体。

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,但都徒劳

我如何完全验证身体?

1 个答案:

答案 0 :(得分:0)

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

  

[          “管理员登录”,          “管理员联系人”,          “管理员”,          “广告视图”       ]

然后,您可以将响应转换为字符串并使用Assert.assertEquals

testers: [
  { _id: 1, username: "John Smith" },
  { _id: 2, username: "Tester_1" },
  { _id: 3, username: "John Doe" }
]

我在本地运行它,对我来说很好

Assert.assertEquals(**response**, "[\n" + 
        "   \"Admin Login\",\n" + 
        "   \"Admin Contact\",\n" + 
        "   \"Administrator\",\n" + 
        "   \"Ads-View\"\n" + 
        "]");