亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Mockito 測試 REST 刪除方法

使用 Mockito 測試 REST 刪除方法

交互式愛情 2023-01-05 16:48:38
我需要幫助使用 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 記錄。
查看完整描述

2 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

您應該將其包裝Void.class在 Mockito 匹配器中:

 when(restTemplate.exchange(
      anyString(), any(HttpMethod.class), any(HttpEntity.class), 
      eq(Void.class), anyMap()))
 .thenReturn(noResponse);

它的工作方式是所有輸入都被ArgumentMatcher包裝或沒有。


查看完整回答
反對 回復 2023-01-05
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

 when(restTemplate.exchange(
      anyString(), any(HttpMethod.class), any(HttpEntity.class), 
      any(Void.class), anyMap()))
 .thenReturn(noResponse);
  • 您不應該在 when().thenReturn() 語句中將 anyMap() 和 anyString() 等螞蟻匹配器與精確值(例如 eq(Void.class))結合起來

  • 你也可以用 any() 替換“Void.class”


查看完整回答
反對 回復 2023-01-05
  • 2 回答
  • 0 關注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號