使用 Mockito 测试 REST 删除方法
Test REST Delete Method using Mockito
我需要帮助使用 Mockito 的正确语法来测试 Spring Rest 模板删除方法。
服务代码:
@Override
public Boolean deleteCustomerItem(String customerNumber, String customerItemId)
throws Exception {
Map<String, String> uriVariables = new HashMap<>();
uriVariables.put("itemId", customerItemId);
try {
ResponseEntity<Void> deleteResponseEntity = restTemplate.exchange( deleteCustomerItemUrl, HttpMethod.DELETE, HttpEntity.EMPTY,
Void.class, uriVariables);
return deleteResponseEntity.getStatusCode().is2xxSuccessful();
} catch (Exception e) {
throw new AppCustomerException(e.getMessage());
}
}
单元测试代码:
@Test
public void testDeleteCustomerItem() throws AppCustomerException {
ResponseEntity<Void> noResponse = new ResponseEntity<Void>(HttpStatus.OK);
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), Void.class, anyMap()))
.thenReturn(noResponse);
Boolean deleteStatus = appCustomerService.deleteCustomerItem("134", "7896");
assertEquals(Boolean.TRUE, deleteStatus);
}
异常:
Mockito Matchers 的使用无效。预期 5 个匹配器 4 个记录。
您应该将 Void.class
包装在 Mockito 匹配器中:
when(restTemplate.exchange(
anyString(), any(HttpMethod.class), any(HttpEntity.class),
eq(Void.class), anyMap()))
.thenReturn(noResponse);
它的工作方式是所有输入都 ArgumentMatcher
包装或 none。
when(restTemplate.exchange(
anyString(), any(HttpMethod.class), any(HttpEntity.class),
any(Void.class), anyMap()))
.thenReturn(noResponse);
你不应该组合任何 anyMap() 和 anyString() 这样的蚂蚁匹配器
在 when().thenReturn() 语句
中具有精确值,例如 eq(Void.class)
你也可以用any()
替换"Void.class"
我需要帮助使用 Mockito 的正确语法来测试 Spring Rest 模板删除方法。
服务代码:
@Override
public Boolean deleteCustomerItem(String customerNumber, String customerItemId)
throws Exception {
Map<String, String> uriVariables = new HashMap<>();
uriVariables.put("itemId", customerItemId);
try {
ResponseEntity<Void> deleteResponseEntity = restTemplate.exchange( deleteCustomerItemUrl, HttpMethod.DELETE, HttpEntity.EMPTY,
Void.class, uriVariables);
return deleteResponseEntity.getStatusCode().is2xxSuccessful();
} catch (Exception e) {
throw new AppCustomerException(e.getMessage());
}
}
单元测试代码:
@Test
public void testDeleteCustomerItem() throws AppCustomerException {
ResponseEntity<Void> noResponse = new ResponseEntity<Void>(HttpStatus.OK);
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), Void.class, anyMap()))
.thenReturn(noResponse);
Boolean deleteStatus = appCustomerService.deleteCustomerItem("134", "7896");
assertEquals(Boolean.TRUE, deleteStatus);
}
异常:
Mockito Matchers 的使用无效。预期 5 个匹配器 4 个记录。
您应该将 Void.class
包装在 Mockito 匹配器中:
when(restTemplate.exchange(
anyString(), any(HttpMethod.class), any(HttpEntity.class),
eq(Void.class), anyMap()))
.thenReturn(noResponse);
它的工作方式是所有输入都 ArgumentMatcher
包装或 none。
when(restTemplate.exchange(
anyString(), any(HttpMethod.class), any(HttpEntity.class),
any(Void.class), anyMap()))
.thenReturn(noResponse);
你不应该组合任何 anyMap() 和 anyString() 这样的蚂蚁匹配器 在 when().thenReturn() 语句
中具有精确值,例如 eq(Void.class)
你也可以用any()
替换"Void.class"