我想模拟请求实体和响应以测试控制器方法上的方法,此代码已由另一个开发人员编写,我应该使用mockito对其进行测试。
我试图模拟请求实体值和respionse实体值,但它不起作用,当我尝试调试时,我得到一个反射错误
public class InquiryController {
private static final Logger log =
LoggerFactory.getLogger(InquiryController.class);
@Autowired
private InquiryProperties inquiryProperties;
@Autowired
private InquiryService inquiryService;
@Autowired
RestTemplate restTemplate;
public static int count = 0;
@Bean
private RestTemplate getRestTemplate() {
return new RestTemplate();
}
@PostMapping(value = "/endCustomer", produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = {
MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<List<EndCustomerDTO>> endCustomer(@RequestBody CustomerInfo customerInfo)
throws IOException, JSONException {
log.info("### InquiryController.endCustomer() ===>");
List<EndCustomerDTO> endCustomerDTOs = null;
try {
//RestTemplate restTemplate = new RestTemplate();
RequestEntity<CustomerInfo> body = RequestEntity.post(new URI(inquiryProperties.getEndCustomer()))
.accept(MediaType.APPLICATION_JSON).body(customerInfo);
ResponseEntity<List<EndCustomerDTO>> response = restTemplate.exchange(body,
new ParameterizedTypeReference<List<EndCustomerDTO>>() {
});
endCustomerDTOs = (response != null ? response.getBody() : new ArrayList<EndCustomerDTO>());
} catch (RestClientException | URISyntaxException e) {
log.error("InquiryController.endCustomer()" + e.getMessage());
}
log.info("### END InquiryController.endCustomer() ===>");
if (null == endCustomerDTOs) {
return new ResponseEntity<List<EndCustomerDTO>>(new ArrayList<EndCustomerDTO>(), HttpStatus.OK);
}
return new ResponseEntity<List<EndCustomerDTO>>(endCustomerDTOs, HttpStatus.OK);
}
桃花长相依
宝慕林4294392
相关分类