我试图用MockRestServiceServer
模拟RestTemplate
。当我要调试测试时,响应实体具有良好的状态和内容类型(以多个状态和内容类型进行测试以检查差异),但是身体是始终 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)
);
最好的问候,
我有完全相同的问题:
尝试将.buffercontent()添加到oberrestserviceserver
final MockRestServiceServer mockRestServiceServer = MockRestServiceServer
.bindTo(requestTemplate)
.bufferContent()
.build()