Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to mock RestTemplate with MockRestServiceServer but body is always null

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,

like image 278
Gugelhupf Avatar asked Mar 01 '17 11:03

Gugelhupf


1 Answers

I had the exact same problem:

Try adding .bufferContent() to the MockRestServiceServer

final MockRestServiceServer mockRestServiceServer = MockRestServiceServer
      .bindTo(requestTemplate)
      .bufferContent()
      .build()
like image 176
Danh Nguyen Avatar answered Nov 14 '22 18:11

Danh Nguyen