'call private method (PowerMockito Test)

First a save() method is executed which passes the test until it reaches a condition, where if it is true it calls the saveBankAccountAndRole() method and if it is false it sends a Mono.error(new Exception("...").

The sizeAccounts(String customerId) method does pass the test.

In the saveBankAccountAndRole(BankAccountDto bnkDto) method, after executing the sizeAccounts() method, the test does not pass, what am I missing?

public Flux<BankAccountDto> findAccountsByCustomerId(String customerId) {
  return mongoRepository
          .findAll()
          .filter(ba ->
                  ba.getCustomerId().equals(customerId))
          .map(AppUtils::entityToDto);
}

private Mono<Long> sizeAccounts(String customerId){
  return findAccountsByCustomerId(customerId)
      .count();
}


private Mono<BankAccountDto> saveBankAccountAndRole(BankAccountDto bnkDto) {
  return sizeAccounts(bnkDto.getCustomerId())
    .flatMap(number -> {
      bnkDto.setOpeningOrder(number + 1);
      return mongoRepository.save(AppUtils.dtoToEntity(bnkDto))
          .switchIfEmpty(Mono.error(new Exception("Problem saving the bank account")))
          .zipWhen(bnk -> {
            var customerRoleDto = new CustomerRoleDto();
            customerRoleDto.setBankAccountId(bnk.getBankAccountId());
            customerRoleDto.setCustomerId(bnkDto.getCustomerId());
            customerRoleDto.setRoleType(bnkDto.getRoleType());

            return webClientRoleHelper.saveCustomerRole(customerRoleDto);
          })
          .switchIfEmpty(Mono.error(new Exception("Problem saving roles")))
          .map(tuple -> AppUtils.entityToDto(tuple.getT1()));
    });
}

test:

@Mock
private IMongoBankAccountRepository mongoRepository;

@InjectMocks
private BankAccountServiceImpl bankAccountServiceImpl;    

@Test
void saveBankAccountAndRoleTest() throws Exception {
    when(mongoRepository.findAll()).thenReturn(Flux.just(bnkDto)
        .map(AppUtils::dtoToEntity));

    when(mongoRepository.findAll().filter(ba ->
        ba.getCustomerId().equals(customerId)))
        .thenReturn(Flux.just(bnkDto).map(AppUtils::dtoToEntity));

    StepVerifier.create(bankAccountServiceImpl.findAccountsByCustomerId(customerId))
        .expectSubscription()
        .expectNext(bnkDto)
        .verifyComplete();

    var spy = PowerMockito.spy(bankAccountServiceImpl);

    PowerMockito.when(spy, "sizeAccounts", customerId)
        .thenReturn(Mono.just(2L));
    PowerMockito.when(spy, "saveBankAccountAndRole",bnkDto)
        .thenReturn(Mono.just(bnkDto));
}

exception:

java.lang.AssertionError: expectation "expectNext(com.nttdata.bootcamp.model.dto.BankAccountDto@147c4523)" failed (expected value: com.nttdata.bootcamp.model.dto.BankAccountDto@147c4523; actual value: com.nttdata.bootcamp.model.dto.BankAccountDto@551725e4) at com.nttdata.bootcamp.business.impl.BankAccountServiceImplTest.saveBankAccountAndRoleTest(BankAccountServiceImplTest.java:267)

Which sends me when verifyComplete()



Solution 1:[1]

By looking at the code in your test, you shouldn't expect an specific object to be returned.

when(mongoRepository.findAll()).thenReturn(Flux.just(bnkDto)
    .map(AppUtils::dtoToEntity));

when(mongoRepository.findAll().filter(ba ->
    ba.getCustomerId().equals(customerId)))
    .thenReturn(Flux.just(bnkDto).map(AppUtils::dtoToEntity));

The code above is mapping that DTO object to an Entity, which makes sense for a repository. However, that means that the following piece of code will "remap" it to a newly created object:

.zipWhen(bnk -> {
     var customerRoleDto = new CustomerRoleDto();
     customerRoleDto.setBankAccountId(bnk.getBankAccountId());
     customerRoleDto.setCustomerId(bnkDto.getCustomerId());
     customerRoleDto.setRoleType(bnkDto.getRoleType());

     return webClientRoleHelper.saveCustomerRole(customerRoleDto);
 })

Thus, you should be expecting an object with that same class, containing the same instance's variables values. But you can't expect it to be the exact same object.

You might want to try this (untested code):

StepVerifier.create(bankAccountServiceImpl.findAccountsByCustomerId(customerId))
    .expectSubscription()
    .expectMatches(dto ->     dto.getBankAccountId().equals(bankDto.getBankAccountId) &&     dto.getCustomerId.equals(bnkDto.getCustomerId))
    .verifyComplete();

I hope that works out for you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 decoiro