如何使用 MockRestServiceServer 测试 RestClientException
2022-09-03 04:48:12
在测试 RestClient-Implementation 时,我想模拟一个 RestClientException,它可能由该实现中的某些 RestTemplate 方法引发,例如 delete-method:
@Override
public ResponseEntity<MyResponseModel> documentDelete(String id) {
template.setErrorHandler(new MyResponseErrorHandler());
ResponseEntity<MyResponseModel> response = null;
try {
String url = baseUrl + "/document/id/{id}";
response = template.exchange(url, DELETE, null, MyResponseModel.class, id);
} catch (RestClientException ex) {
return handleException(ex);
}
return response;
}
我怎样才能做到这一点?
我以这种方式定义模拟服务器:
@Before
public void setUp() {
mockServer = MockRestServiceServer.createServer(template);
client = new MyRestClient(template, serverUrl + ":" + serverPort);
}