簡體   English   中英

Spring 單元測試 [webflux,雲]

[英]Spring unit tests [webflux, cloud]

我是單元測試主題的新手,我的問題是我是否應該對方法的每一行代碼執行測試,或者我可以通過哪些方式執行這些測試以獲得良好的覆蓋率,如果也是,是否應該例外評估與否?

例如,如果我有這個服務方法,它也使用一些與其他微服務通信的助手,有人可以給我如何執行的例子,非常感謝。

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("....."));
      });

}

鑒於您想對該代碼進行單元測試,您需要模擬諸如webClientCustomer之類的依賴項。

然后,您應該始終測試代碼中的相關路徑。 查看您的代碼,我只看到三個要測試的相關代碼:

  • 如果webClientCustomer.findCustomerById(bankAccount.getCustomerId());該方法返回一個空的Mono 返回一個空的Mono
  • saveBankAccountAndRole(bankAccount)並且您的save()方法實際上返回saveBankAccountAndRole(bankAccount)返回的任何內容。 如果webClientCustomer.isCustomerAuthorized(customerType, accountType, sizeAccounts)true ,就會發生這種情況;
  • 如果webClientCustomer.isCustomerAuthorized(customerType, accountType, sizeAccounts)false ,則該方法返回異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM