简体   繁体   English

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

[英]Trying to mock RestTemplate with MockRestServiceServer but body is always null

I am trying to mock RestTemplate with MockRestServiceServer . 我试图嘲弄RestTemplateMockRestServiceServer 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 . 当我调试我的测试时,响应实体具有良好的状态和内容类型(使用多个状态和内容类型进行测试以检查差异),但是主体始终为 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, 最好的祝福,

I had the exact same problem: 我有同样的问题:

Try adding .bufferContent() to the MockRestServiceServer 尝试将.bufferContent()添加到MockRestServiceServer

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM