API Response Validations
You can perform assertions and verifications on API responses using the RestValidationsBuilder. Access it through the SHAFT.API instance or the standalone Validations class.
isEqualToFileContent()
Checks that the response body is equal to the content of an expected file.
// Using SHAFT.API instance (recommended)
api.assertThatResponse().isEqualToFileContent("src/test/resources/expected-response.json").perform();
api.verifyThatResponse().isEqualToFileContent("src/test/resources/expected-response.json").perform();
// Using standalone validation
Validations.assertThat().response(response).isEqualToFileContent("src/test/resources/expected-response.json").perform();
doesNotEqualFileContent()
Checks that the response body is not equal to the content of an expected file.
api.assertThatResponse().doesNotEqualFileContent("src/test/resources/old-response.json").perform();
containsFileContent()
Checks that the response body contains the content of an expected file.
api.assertThatResponse().containsFileContent("src/test/resources/partial-response.json").perform();
doesNotContainFileContent()
Checks that the response body does not contain the content of an expected file.
api.assertThatResponse().doesNotContainFileContent("src/test/resources/excluded-content.json").perform();
extractedJsonValue()
Extracts a value from the JSON response using a JSONPath expression and validates it. Chain a comparison method such as .isEqualTo(), .contains(), or .matchesRegex().
// Validate extracted JSON value
api.assertThatResponse().extractedJsonValue("data.name").isEqualTo("John").perform();
api.verifyThatResponse().extractedJsonValue("data.items[0].id").isEqualTo("123").perform();
JSONPath expressions should be provided without the leading $. prefix. For example, use "data.name" instead of "$.data.name". SHAFT uses the Jayway JsonPath library (the standard Java JSONPath implementation) for expression evaluation.
extractedJsonValueAsList()
Extracts a list of values from the JSON response using a JSONPath expression and validates it.
api.assertThatResponse().extractedJsonValueAsList("data.items").isNotNull().perform();
matchesSchema()
Checks that the response body matches a JSON schema defined in an expected file.
api.assertThatResponse().matchesSchema("src/test/resources/response-schema.json").perform();
api.verifyThatResponse().matchesSchema("src/test/resources/response-schema.json").perform();
doesNotMatchSchema()
Checks that the response body does not match a JSON schema defined in an expected file.
api.assertThatResponse().doesNotMatchSchema("src/test/resources/wrong-schema.json").perform();
When using SHAFT.API, the response from the last request is automatically passed to the validation builder. You don't need to pass it explicitly.