我是單元測驗主題的新手,我的問題是我是否應該對方法的每一行代碼執行測驗,或者我可以通過哪些方式執行這些測驗以獲得良好的覆寫率,如果也是,是否應該例外評估與否?
例如,如果我有這個服務方法,它也使用一些與其他微服務通信的助手,有人可以給我如何執行的例子,非常感謝。
public Mono<BankAccountDto> save(BankAccountDto bankAccount) {
var count = findAccountsByCustomerId(bankAccount.getCustomerId()).count();
var customerDto = webClientCustomer
.findCustomerById(bankAccount.getCustomerId());
var accountType = bankAccount.getAccountType();
return customerDto
.zipWith(count)
.flatMap(tuple -> {
final CustomerDto custDto = tuple.getT1();
final long sizeAccounts = tuple.getT2();
final var customerType = custDto.getCustomerType();
if (webClientCustomer.isCustomerAuthorized(customerType, accountType, sizeAccounts)) {
return saveBankAccountAndRole(bankAccount);
}
return Mono.error(new Exception("....."));
});
}
編輯
public Mono<BankAccountDto> save(BankAccountDto bankAccount) {
var count = findAccountsByCustomerId(bankAccount.getCustomerId()).count();
var customerDto = webClientCustomer
.findCustomerById(bankAccount.getCustomerId());
return customerDto
.zipWith(count)
.flatMap(tuple -> {
final var customDto = tuple.getT1();
final var sizeAccounts = tuple.getT2();
final var accountType = bankAccount.getAccountType();
// EDITED
return webClientCustomer.isCustomerAuthorized(customDto, accountType, sizeAccounts)
.flatMap(isAuthorized -> {
if (Boolean.TRUE.equals(isAuthorized)) {
return saveBankAccountAndRole(bankAccount);
}
return Mono.error(new Exception("No tiene permisos para registrar una cuenta bancaria"));
});
});
}
uj5u.com熱心網友回復:
鑒于您想對該代碼進行單元測驗,您需要模擬依賴項,例如webClientCustomer.
然后,您應該始終測驗代碼中的相關路徑。查看您的代碼,我只看到三個要測驗的相關代碼:
Mono如果回傳空,則該方法回傳webClientCustomer.findCustomerById(bankAccount.getCustomerId());空Mono;saveBankAccountAndRole(bankAccount)被呼叫并且您的save()方法實際上回傳任何saveBankAccountAndRole(bankAccount)回傳。webClientCustomer.isCustomerAuthorized(customerType, accountType, sizeAccounts)如果是,這應該會發生true;webClientCustomer.isCustomerAuthorized(customerType, accountType, sizeAccounts)如果是,該方法回傳例外false。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/419243.html
標籤:
