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

时间:2017-03-01 11:50:28

标签: java spring unit-testing null resttemplate

我正在尝试使用RestTemplate模仿MockRestServiceServer。当我调试我的测试时,响应实体具有良好的状态和内容类型(使用多个状态和内容类型进行测试以检查差异),但正文始终 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)
    );

致以最诚挚的问候,

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题:

尝试将.bufferContent()添加到MockRestServiceServer

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