In the example below I am trying to send a request with a JSON body to a mock REST API. The problem is when I try to get the response which should be JSON it returns an empty or null string. Why is this the case?
Note: when I add the .peek() to the response object it will print the expected results for me. But what I want is to capture those results in the response object. Also this one happens with the JSON string sent is bad format. I am expecting the bad results so that I can assert on them.
String jsonBody = ...
MockMvcRequestSpecification request = given()
.header("Content-Type", this.contentType)
.body(jsonBody);
ResponseOptions response = given().spec(request)
.post(this.apiUrlPath);
//This works, I can see the status code
assertThat(response.statusCode(), Matchers.equalTo(400));
//This doesn't print anything, the body is always empty
//json response is expected explaining why the request failed
//response.peek() will print what I am expecting
System.out.println(response.getBody().asString());