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

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

如何計算使用 Junit 和 Mockito 調用方法的次數?

如何計算使用 Junit 和 Mockito 調用方法的次數?

慕少森 2021-07-12 13:07:29
我有這個方法要在 ProductService 中測試:@Override    public void validateUpdate(Product product, Product modifiedProduct, List<FieldError> errors) throws AppException {        if(modifiedProduct == null || product == null) {            addError(errors, "product", "Product cannot be null");        }else {            validateName(modifiedProduct.getName(), errors);            validateShortDescription(modifiedProduct.getShortDescription(), errors);            validateDescription(modifiedProduct.getDescription(), errors);            validateRegularPriceAndPromotionprice(modifiedProduct.getRegularPrice(), modifiedProduct.getPromotionPrice(), errors);            validateCategory(product.getCategory(), errors);            validateCategoryMatches(product.getCategory(), modifiedProduct.getCategory(), errors);            validateStore(product.getStore(), errors);            validateSku(modifiedProduct.getSku(), errors);            validateWeight(modifiedProduct.getWeight(), errors);            validateQuantityInStock(modifiedProduct.getQuantityInStock(), errors);            validateNotifyLowStock(modifiedProduct.getNotifyLowStock(), errors);        }    }但我只想創建一個測試來驗證是否正在調用所有方法。第一個是查看 addError 方法是否被調用一次:@Test    public void testValidateUpdateProduct() {        ProductService productService = Mockito.mock(ProductService.class);        List<FieldError> errors = new ArrayList<FieldError>();        productService.validateUpdate(null, null, errors);        Mockito.verify(productService, Mockito.times(1)).addError(errors, "product", "Product cannot be null");    }但后來我得到:Wanted but not invoked:productService.addError(    [],    "product",    "Product cannot be null");-> at ca.edooby.edoobyapi.service.ProductServiceTest.testValidateUpdateProduct2(ProductServiceTest.java:1022)However, there was exactly 1 interaction with this mock:productService.validateUpdate(    ca.edooby.edoobyapi.model.Product@d2ca3a9,    ca.edooby.edoobyapi.model.Product@2b26d289,    []);
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 352 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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