6

I am trying to mock RestTemplate with MockRestServiceServer. When I am debugging my test, the response entity has the good status and content-type (tested with multiple status and content-type to check the differences), but the body is always null.

    final String uri = "/uri";
    final String notNullJsonString = "{}";
    // restTemplate is autowired
    final MockRestServiceServer mockRestServiceServer = MockRestServiceServer.createServer(restTemplate);
    mockRestServiceServer.expect(
        MockRestRequestMatchers.requestTo(new URI(uri))
    ).andRespond(
        MockRestResponseCreators.withStatus(HttpStatus.ACCEPTED)
            .contentType(MediaType.APPLICATION_JSON)
            .body(notNullJsonString)
    );

Best regards,

gokan
  • 1,028
  • 1
  • 14
  • 30

1 Answers1

16

I had the exact same problem:

Try adding .bufferContent() to the MockRestServiceServer

final MockRestServiceServer mockRestServiceServer = MockRestServiceServer
      .bindTo(requestTemplate)
      .bufferContent()
      .build()
Danh Nguyen
  • 161
  • 1
  • 3