2

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());
TotemPole
  • 81
  • 1
  • 2
  • 8
  • I'm still investigating, but this other stack question seems to be talking about the same thing: http://stackoverflow.com/questions/28324994/empty-exception-body-in-spring-mvc-test – TotemPole Jan 13 '17 at 15:52
  • Also still investigating, this link from spring mvc test is explaining end-to-end integration testing vs using the mock: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#spring-mvc-test-vs-end-to-end-integration-tests – TotemPole Jan 13 '17 at 15:54
  • Which version of RestAssured are you using? I can't find the `peek` method. – Adam Jan 20 '17 at 19:45

0 Answers0