在下面的示例中,我尝试将带有JSON正文的请求发送到模拟REST API。问题是当我尝试获取应该是JSON的响应时,它返回一个空字符串或空字符串。为什么会这样?
注意:当我将.peek()添加到响应对象时,它将为我打印预期的结果。但我想要的是在响应对象中捕获这些结果。此外,发送的JSON字符串也是格式错误。我期待糟糕的结果,以便我可以对它们断言。
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());