Describe the bug
"org.springframework.cloud.contract" version "2.2.3.RELEASE"
SCC doesn't generate RestAssured-asserts in field _response:matchers:body:_ if there is no asserts in field _response:body:_ in file of YAML-contract.
Sample
Case 1 - field response:body: exists:
description: >
Sends one JSON request and returns one response with the "ERROR" status
name: Sending a request without the required fields requestId and responseId receiving an unsuccessful response
request:
method: POST
url: /rest/request
headers:
Content-type: application/json
body:
requestId: null
responseId: null
response:
async: true
status: 400
headers:
Content-Type: application/json
body:
errorDescription: null
matchers:
body:
- path: $.errorDescription
type: by_regex
value: "(responseId can't be null, requestId can't be null|requestId can't be null, responseId can't be null)"
The the result is as expected:
@Test
public void validate_sending_a_request_without_the_required_fields_requestId_and_responseId_receiving_an_unsuccessful_response() throws Exception {
// given:
RequestSpecification request = given()
.header("Content-type", "application/json")
.body("{\"requestId\":null,\"responseId\":null}");
// when:
Response response = given().spec(request)
.post("/rest/request");
// then:
assertThat(response.statusCode()).isEqualTo(400);
assertThat(response.header("Content-Type")).isEqualTo("application/json");
// and:
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).field("['errorDescription']").isNull();
// and:
assertThat(parsedJson.read("$.errorDescription", String.class)).matches("(responseId can't be null, requestId can't be null|requestId can't be null, responseId can't be null)");
}
Case 2 - field response:body: doesn't exists:
description: >
Sends one JSON request and returns one response with the "ERROR" status
name: Sending a request without the required fields requestId and responseId receiving an unsuccessful response
request:
method: POST
url: /rest/request
headers:
Content-type: application/json
body:
requestId: null
responseId: null
response:
async: true
status: 400
headers:
Content-Type: application/json
matchers:
body:
- path: $.errorDescription
type: by_regex
value: "(responseId can't be null, requestId can't be null|requestId can't be null, responseId can't be null)"
The result is not what was expected:
@Test
public void validate_sending_a_request_without_the_required_fields_requestId_and_responseId_receiving_an_unsuccessful_response() throws Exception {
// given:
RequestSpecification request = given()
.header("Content-type", "application/json")
.body("{\"requestId\":null,\"responseId\":null}");
// when:
Response response = given().spec(request)
.post("/rest/request");
// then:
assertThat(response.statusCode()).isEqualTo(400);
assertThat(response.header("Content-Type")).isEqualTo("application/json");
}
Where did this RestAssured-code go in 2nd case?
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThat(parsedJson.read("$.errorDescription", String.class)).matches("(responseId can't be null, requestId can't be null|requestId can't be null, responseId can't be null)");
Please learn how to properly format the code
Please learn how to properly format the code
Sorry, I fixed everything :)
There's no body element in the response so there's nothing to match against.
There's no body element in the response so there's nothing to match against.
Thanks for your reply! How can I only do a regex-check (response:matchers:body)?
This behavior is slightly unobvious.
You can't do a regex of sth that is not inside the body of the response. That makes no sense.
You can't do a regex of sth that is not inside the body of the response. That makes no sense.
Brainexploding!
Adding one field does the trick:
description: >
Sends one JSON request and returns one response with the "ERROR" status
name: Sending a request without the required fields requestId and responseId receiving an unsuccessful response
request:
method: POST
url: /rest/request
headers:
Content-type: application/json
body:
requestId: null
responseId: null
response:
async: true
status: 400
headers:
Content-Type: application/json
body:
errorDescription: $.errorDescription
matchers:
body:
- path: $.errorDescription
type: by_regex
value: "(responseId can't be null, requestId can't be null|requestId can't be null, responseId can't be null)"
Code:
public void validate_sending_a_request_without_the_required_fields_requestId_and_responseId_receiving_an_unsuccessful_response() throws Exception {
// given:
RequestSpecification request = given()
.header("Content-type", "application/json")
.body("{\"requestId\":null,\"responseId\":null}");
// when:
Response response = given().spec(request)
.post("/rest/request");
// then:
assertThat(response.statusCode()).isEqualTo(400);
assertThat(response.header("Content-Type")).isEqualTo("application/json");
// and:
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
// and:
assertThat(parsedJson.read("$.errorDescription", String.class)).matches("(responseId can't be null, requestId can't be null|requestId can't be null, responseId can't be null)");
String responseBody = response.getBody().asString();
}
The issue is resolved, thank you :)