REST Assured:如何测试响应体中Set类型的对象?
Hey there! Let's break down why you're running into that "parameter regionsSet is:" error when asserting your REST Assured response. From the response sample you provided, the key issue jumps out pretty quickly—let's walk through fixes step by step.
The error message is hinting that REST Assured can't find a field named regionsSet in your response. Looking at your response example:
{regions=[1, 2], endDate=2017-05-01T22:00:00, productIds=[], bannerId=15,...}
Your response uses the field name regions, not regionsSet. It's almost certainly a typo in your assertion code, or you might be trying to convert the regions array into a Set and referencing it incorrectly.
Let's cover the two most common scenarios:
1. Fix the field name typo
If you just need to assert the contents of the regions array directly, use the correct field name in your REST Assured assertion. Use Hamcrest matchers for clean, readable assertions:
- To check that
regionscontains specific values:given() .when() .get("/your-endpoint") .then() .statusCode(200) .body("regions", hasItems(1, 2)) // Verifies both 1 and 2 are present .body("regions.size()", equalTo(2)); // Verifies the array has exactly 2 elements - To check that
regionsis exactly the array [1,2]:.body("regions", equalTo(Arrays.asList(1, 2)))
2. Convert regions to a Set for assertion
If you specifically need to work with a Set (e.g., to ignore order or check uniqueness), extract the field first and convert it to a Set before asserting:
Set<Integer> regionsSet = given() .when() .get("/your-endpoint") .then() .statusCode(200) .extract() .body() .jsonPath() .getSet("regions", Integer.class); // Now assert the Set assertThat(regionsSet, containsInAnyOrder(1, 2)); assertThat(regionsSet.size(), equalTo(2));
If you're still seeing the error, double-check your assertion code for any references to regionsSet—it's easy to leave a typo in there. You can also print the full response body first to confirm all field names match what you're expecting:
String responseBody = given() .when() .get("/your-endpoint") .then() .extract() .asString(); System.out.println("Full Response: " + responseBody);
内容的提问来源于stack exchange,提问作者nima pirmoradian

