java – 尝试使用MockRestServiceServer模拟RestTemplate,但body始终为null

我试图用MockRestServiceServer模拟RestTemplate.当我调试我的测试时,响应实体具有良好的状态和内容类型(使用多个状态和内容类型进行测试以检查差异),但是主体始终为空.

    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)
    );

最好的祝福,

最佳答案 我有同样的问题:

尝试将.bufferContent()添加到MockRestServiceServer

final MockRestServiceServer mockRestServiceServer = MockRestServiceServer
      .bindTo(requestTemplate)
      .bufferContent()
      .build()
点赞