java - 创建虚拟请求实体对象以在模拟测试请求 .post 方法时返回
问题描述
我正在尝试模拟一个请求实体对象来测试RequestEntity.post方法。
RequestEntity<CustomerInfo> body = RequestEntity.post(new
URI(inquiryProperties.getEndCustomer()))
.accept(MediaType.APPLICATION_JSON).body(customerInfo);
我想使用 mockito 来模拟这个方法,并且在“when”中我想为此返回一个虚拟对象。
这是我试图为控制器模拟的方法。
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();
}
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);
}
解决方案
用于模拟 REST 模板交换和监视响应实体的 Mockito 示例
@RunWith(MockitoJUnitRunner.class)
public class CustomerTest {
@Mock
RestTemplate restTemplate;
@Spy
ResponseEntity responseEntity = mock(ResponseEntity.class);
@Inject
InquiryService inquiryService;
@Test
public void endCustomerTest() {
EndCustomerDTO yourEndCustomerDTO= new EndCustomerDTO();
//define the entity you want the exchange to return
ResponseEntity<List<EndCustomerDTO >> yourEndCustomerDTOEntity = new ResponseEntity<List<EndCustomerDTO >>(HttpStatus.ACCEPTED);
Mockito.when(restTemplate.exchange(
Matchers.eq("/urlPattern/urlPath"),
Matchers.eq(HttpMethod.POST),
Matchers.<HttpEntity<List<EndCustomerDTO >>>any(),
Matchers.<ParameterizedTypeReference<List<EndCustomerDTO >>>any())
).thenReturn(yourEndCustomerDTOEntity);
//service call to get the end customers
List<EndCustomerDTO > result = inquiryService.getEndCustomers();
//asserting by comparing expected and actual value
Assert.assertEquals(yourEndCustomerDTOEntity , result .get(0));
}
}
推荐阅读
- visual-studio-code - 无法将 mysql 与 Visual Studio Code 连接
- r - 函数将 POSIXct 日期转换为数字
- arrays - 如何将 char 数组转换为 BST?
- machine-learning - 在 Tensorflow.js 中使用卷积层预处理图像
- sql - 将 DateTimePicker 时间格式值设置为从数据库 VB.NET 返回的值
- laravel - Laravel SMTP 邮件发送
- r - R 中的 if、else if、else 语句和逻辑运算符以及创建函数
- sql - 找出 2 个表之间的差异
- c# - 如何构建在unity/c#中导入的c++ dll
- string - 我无法使用模板在一个字符串中获取两个字符串值