I think de-serializing the Json array to a list of Section
objects will be easy to use Hamcrest Matchers. You have to override the equals()
method in order to check whether Section
object is available in the created Section
list.
Section.java
public class Section {
private String name;
private String description;
public Section(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
final Section other = (Section) obj;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if (!this.description.equals(other.description)) {
return false;
}
return true;
}
}
Then use any Hamcrest Matcher you prefer.
RestAssured.baseURI="https://run.mocky.io/v3/bc929050-35ba-42a1-86b1-58f3fd1ead83";
List<Section> sections = RestAssured.given()
.when()
.get()
.then()
.extract().jsonPath().getList("sections", Section.class);
Section sectionA = new Section("A", "aaa");
Section sectionB = new Section("B", "bbb");
List<Section> sectionList = new ArrayList<>();
sectionList.add(sectionA);
sectionList.add(sectionB);
assertThat(sections, hasItem(sectionA)); // pass
assertThat(sections, hasSize(2)); // pass
assertThat(sections, hasItem(hasProperty("name", equalTo("B")))); // pass
assertThat(sections, equalTo(sectionList)); // pass
assertThat(sections, containsInAnyOrder(sectionList.toArray())); // pass