例如,我有JSON作为回应:
[{"id":1,"name":"text"},{"id":2,"name":"text"}]}
我想验证响应是否包含自定义对象.例如:
Person(id=1, name=text)
我找到了解决方案
Person[] persons = response.as(Person[].class);
assertThat(person, IsArrayContaining.hasItemInArray(expectedPerson));
我想要这样的东西:
response.then().assertThat().body(IsArrayContaining.hasItemInArray(object));
这有什么解决方案吗?
在此先感谢您的帮助!
解决方法:
body()方法接受路径和Hamcrest匹配器(参见javadocs).
所以,你可以这样做:
response.then().assertThat().body("$", customMatcher);
例如:
// 'expected' is the serialised form of your Person
// this is a crude way of creating that serialised form
// you'll probably use whatever JSON de/serialisaiotn library is in use in your project
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("id", 1);
expected.put("name", "text");
response.then().assertThat().body("$", Matchers.hasItem(expected));