[英]Mock resttemplate
ResponseEntity<List<AgreementRecord>> myEntity = new
ResponseEntity<List<AgreementRecord>>(HttpStatus.ACCEPTED);
when(restTemplate.exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.<HttpEntity<?>>any(),
ArgumentMatchers.<Class<?>>any())).thenReturn(myEntity);
其余模板从应用程序返回列表Eclipse抛出编译错误
类型为OngoingStubbing>的thenReturn(ResponseEntity)方法不适用于参数(ResponseEntity>)
休息模板
ResponseEntity<List<AgreementRecord>> responseEntity =
restTemplate.exchange(smoUrl+ GET_AGREEMENT_RECORDS + customerId
,HttpMethod.GET,null,new
ParameterizedTypeReference<List<AgreementRecord>>() {
});
responseEntity.getBody();
您可以做的一件事就是通过doReturn()避开Mockito的编译时类型检查。
doReturn(myEntity)
.when(restTemplate)
.exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class)
ArgumentMatchers.<HttpEntity<?>>any(),
ArgumentMatchers.<Class<?>>any()));
这将返回myEntity而不进行编译时类型检查。 如果您输入的类型错误,则会在运行测试后立即找出。
编辑:
@Test()
public void test() {
class AgreementRecord {}
ResponseEntity<List<AgreementRecord>> myEntity = new ResponseEntity<>(
Arrays.asList(new AgreementRecord()), HttpStatus.ACCEPTED);
doReturn(myEntity)
.when(restTemplate)
.exchange(
anyString(),
any(HttpMethod.class),
any(),
any(ParameterizedTypeReference.class));
ResponseEntity<List<AgreementRecord>> responseEntity = restTemplate.exchange(
"url", HttpMethod.GET, null, new ParameterizedTypeReference<List<AgreementRecord>>() {});
List<AgreementRecord> body = responseEntity.getBody();
assertTrue(responseEntity.getStatusCode().is2xxSuccessful());
assertNotNull(body);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.