Spring unit tests [webflux, cloud]

I am new to the topic of unit testing and my question is whether I should perform the test as such of each line of code of a method or in what ways I can perform these tests to have a good coverage, if also, should exceptions be evaluated or not?

If for example I have this service method that also uses some helpers that communicate with other microservices, someone could give me examples of how to perform, thank you very much.

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

}

EDIT

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

  }

Given that you want to unit test this code, you would need to mock dependencies such as webClientCustomer.

Then you should always test whatever are the relevant paths within the code. Looking at your code I only see three relevant ones to be tested:

  • the method returns an empty Mono if webClientCustomer.findCustomerById(bankAccount.getCustomerId()); returns an empty Mono;
  • saveBankAccountAndRole(bankAccount) is called and your save() method actually returns whatever saveBankAccountAndRole(bankAccount) returns. This would should happen if webClientCustomer.isCustomerAuthorized(customerType, accountType, sizeAccounts) is true;
  • the method returns an exception if webClientCustomer.isCustomerAuthorized(customerType, accountType, sizeAccounts) is false.